ruby - Trying to define a method within a method within a class -
i'm trying spec pass , i'm receiving error, "undefined method `first_word' "inferno":string"
i have defined 'first_word' method, within class method 'title'. i'm having problem determining how can phone call 'first_word' method on string within class method 'title'.
the spec:
describe 'title' 'should capitalize first letter' @book.title = "inferno" @book.title.should == "inferno" end
it 'should capitalize every word' @book.title = "stuart little" @book.title.should == "stuart little" end describe 'should capitalize every word except...' describe 'articles' specify 'the' @book.title = "alexander great" @book.title.should == "alexander great" end specify 'a' @book.title = "to kill mockingbird" @book.title.should == "to kill mockingbird" end specify 'an' @book.title = "to eat apple day" @book.title.should == "to eat apple day" end end specify 'conjunctions' @book.title = "war , peace" @book.title.should == "war , peace" end specify 'prepositions' @book.title = "love in time of cholera" @book.title.should == "love in time of cholera" end end describe 'should capitalize...' specify 'i' @book.title = "what wish knew when 20" @book.title.should == "what wish knew when 20" end specify 'the first word' @book.title = "the man in iron mask" @book.title.should == "the man in iron mask" end end
my code:
class book attr_accessor :title def initialize @title end def title=(str) def first_word self[0,1].capitalize + self[1,-1] end cap_except = ["over","and","of","a","to","the","an","or","but","if","else","in"] str = str.split.map {|w| cap_except.include?(w) ? w : w.capitalize}.join(" ").first_word @title = str end
end
you're calling first_word
on string, similar "foo".length
.
you haven't defined first_word
on string
class.
you have defined method phone call with string, e.g., first_word("foo")
.
unrelated, why trying nest method definitions this?
ruby class methods specifications
No comments:
Post a Comment