4 ms·
In Ruby, things are more complicated. I need an ampersand to pass a function and brackets to call it: Proc calling is built directly into the language via the
by carbon8 16y ago
In Ruby, things are more complicated. I need an ampersand to pass a function and brackets to call it:
Proc calling is built directly into the language via the yield keyword and it's by far the most common way to call Procs.
More importantly (and annoyingly), if I define a top-level method with def—let’s call those top-level def methods (TLDMs)—Ruby won’t let me pass it as a block to any other method.
First, you can indeed pass a method as an argument using &method(:method_name). Secondly, it's rare to pass around named methods in Ruby (http://news.ycombinator.com/item?id=1141245 http://news.ycombinator.com/item?id=1141245) because Procs/blocks are flexible enough to make it almost always unnecessary. See also: http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/ http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-orie...