|
|
|
|
|
by dragonwriter
2895 days ago
|
|
> However we can't pass a function as argument Right, Ruby doesn't have functions at all, so you can't pass them. This is why Ruby isn't simply better than Python here, though I'd say it's still mostly better. > We can pass a symbol with the name of the fuction. No, you can either convert the name to a callable and pass that, or for some methods you can pass the name of a method you'd like called on an object that the called method will get somewhere else. This isn’t esuivslent to having the name of a function. Instead, its a facility that is usually offered by a method to provide a more concise alternative to passing an equivalent block for a no-arg method. E.g.: enum.inject(start, :foo)
is equivalent to: enum.inject(start) { |obj| obj.send(:foo) }
If you actually have a function-like (callable) object (bound method object or proc/lambda) you can send that as a regular argument, or if the method it is passed to expects a block (as is more frequently the case for Ruby methods that need a single function-like argument) convert the callable to a block with the “&” prefix and send that. |
|
I think they're referring to Symbol#to_proc.