RFS: Array Map and Multiple Assignments

preview_player
Показать описание
We show you how to clean up the code from the previous episode using Array#map and Multiple Assignments

Рекомендации по теме
Комментарии
Автор

Good one. I like multiple assignments.

venkatch
Автор

That code only does the same thing if the input has two names, of course - nothing in it takes "James Earl Carter" and returns the same thing as the orginal code...

thomasoa
Автор

collect could also have been used instead of map and would have produced the same result.

stpaquet
Автор

can you explain the difference between map and each?  I tried some but I don't know want this means.  

2.1.1 :001 > a = ["a", "b", "c"];
2.1.1 :002 >   puts a.map {|item| "map_" +item}
map_a
map_b
map_c
 => nil 
2.1.1 :003 > puts a.each {|item| "map_" +item}
a
b
c
 => nil 
2.1.1 :004 >  a.each do |n| puts "map"+n end
mapa
mapb
mapc
 => ["a", "b", "c"] 
2.1.1 :005 >  a.map do |n| puts "map"+n end
mapa
mapb
mapc
 => [nil, nil, nil] 

#ruby   #map  vs #each  

stefanmaier
Автор

is better using a variable name like "word" instead than "n", "word" is more meaningful... I think.

estebancastro
visit shbcf.ru