3 ms·
Wouldn't it make more sense to use each rather than map for that example? %w{apple pear banana}.each {|f| puts "#{f} is a fruit."} => apple is a fruit
by harpastum 17y ago
Wouldn't it make more sense to use each rather than map for that example?
%w{apple pear banana}.each {|f| puts "#{f} is a fruit."}
=> apple is a fruit
=> pear is a fruit
=> banana is a fruit
Map is for creating a new array with each of the results in it, like this:
%w{apple pear banana}.map {|f| "#{f} is a fruit."}
=>["apple is a fruit.", "pear is a fruit.", "banana is a fruit."]
which would return an array with each of the strings in it.
- jamesbritt 17y agoQuite right. puts %w{apple pear banana}.map {|f| "#{f} is a fruit."}.join(' And ')