Thanks, very usefull to perform basic skills. What the difference between each and map? I guess that map has fewer methods or something like that.
Great question! The difference is in what they return.
When working with hashes each will run a block of code for each key/value pair in your hash and when it is done it will return the original hash
each
irb:> { a: 1, b: 2}.each{|k, v| v*3 } => {:a=>1, :b=>2}
map for a hash(just like an array) will return a new array with the result of executing your block
map
irb> { a: 1, b: 2}.map{|k, v| v*3 } => [3, 6]
TL;DR
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks, very usefull to perform basic skills. What the difference between each and map? I guess that map has fewer methods or something like that.
Great question! The difference is in what they return.
When working with hashes
each
will run a block of code for each key/value pair in your hash and when it is done it will return the original hashmap
for a hash(just like an array) will return a new array with the result of executing your blockTL;DR