We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

John • 2 years ago

Fantastic explanation!

Husoski • 3 years ago

Thanks for posting this. I'm still baffled at how to find out anything about the specific usage .map(&:method) in anything like official docs. I've looked for it at docs.ruby-lang.org and at ruby-doc.org, and both say that that Enumerable#map takes no arguments at all (same for Array#map). How would anyone ever find out that something like this even exists?

Victor Skyba • 6 years ago

thanks!

Romenig Lima Damasio • 7 years ago

nice!

Tomasz Drgas • 7 years ago

[].map(&MyClass.new) # why did it even do anything? Isn't .map suppose to call the (&MyClass.new) for each element of [] this array.. It is empty so I was thinking it will call (&MyClass.new) 'nil' times

Larry Reaves • 6 years ago

Map will call the passed proc 0 times. Prefixing the MyClass instance returned by .new with a & calls the to_proc method on that MyClass instance and the proc returned from that function is then passed to map as a parameter. It happens before map is invoked so map can't short circuit it to avoid creating the proc.

Words_Are_Wind • 7 years ago

Thanks for this. It helped a lot.

Kanwardeep Singh Baweja • 8 years ago

Awesome explanation!

Panayotis Matsinopoulos • 8 years ago

Can I call a method that takes arguments? "[...].each(&:method, arg1, arg2)" ?

Lemongrass • 5 years ago
Julien • 7 years ago

Curious for this too.

Hanaa Alshareef • 8 years ago

Thanks for your explanation

Davide Ghezzi • 9 years ago

just WOW!

Andres Cuervo • 10 years ago

Thanks, this was such a nice explanation, I needed a refresher!

Alex • 10 years ago

Amazing and simple explanation, thank you!