Ruby puts command in modules returning nil -
i working though learntocodethehardway.com http://ruby.learncodethehardway.org/book/ex25.html on ex25. in example there module has bunch of methods return or print values. print_last_word method when supplied array of strings puts nil. in example output. question why?
to precise, doesn't puts nil
- puts last word , returns nil
. here's example output:
>> ex25.print_last_word(words) wait. # <- output => nil # <- return value
puts
returns nil
.
update
there seems bug in print_first_word
:
module ex25 def ex25.print_first_word(words) word = words.pop(0) puts word end end ex25.print_first_word(["foo", "bar", "baz"]) #=> nil
this because ["foo", "bar", "baz"].pop(0)
returns empty array , puts []
returns nil
without printing anything.
a working implementation (in exercise's style) this:
module ex25 def ex25.print_first_word(words) word = words.shift puts word end end
Comments
Post a Comment