ruby on rails - content_tag with array and ul, li tags - map vs each -


i have below code in helper:

arr = ['one', 'two', 'three']  errors = content_tag :ul   arr.map |msg|     content_tag :li, msg   end.join.html_safe end 

it works fine, in first version tried each instead of map , didn't work. explain me why? (each version generated <ul>onetwothree</ul>)

this because array#each returns receiver on called.but array#map returns new array containing values returned block.

arr = ['one', 'two', 'three']  arr.each {|i|  + 'weak' }.join(" ") # "one 2 three" arr.map {|i|  + 'weak' }.join(" ") #"oneweak twoweak threeweak" 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

php - Add the correct number of days for each month -