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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -