On 6 Feb 2009, at 15:58, Kenneth McDonald wrote:

>
> OK, I see how this works now, but I can't figure out how to get extra
> args in
> there, i.e. something like [1, 2, 3].collect(&:modulo, 2) doesn't work
> because
> it's improper Ruby syntax. I assume there's a reason extra args are
> allowed,
> so could someone give a brief illustration of the calling convention?
>
I don't think you can - you just have to use the longhand syntax in  
those cases.

Fred


> Thanks in advance,
> Ken
>
>
> On Feb 5, 2009, at 6:13 PM, [email protected] wrote:
>
>>
>> It is not possible in "plain" ruby because Rails extends the Symbol
>> class with the possibility of converting it into a Proc (that's what
>> happens when you precede something with a &). From the Rails source
>> code:
>>
>> unless :to_proc.respond_to?(:to_proc)
>> class Symbol
>>   # Turns the symbol into a simple proc, which is especially useful
>> for enumerations. Examples:
>>   #
>>   #   # The same as people.collect { |p| p.name }
>>   #   people.collect(&:name)
>>   #
>>   #   # The same as people.select { |p| p.manager? }.collect { |p|
>> p.salary }
>>   #   people.select(&:manager?).collect(&:salary)
>>   def to_proc
>>     Proc.new { |*args| args.shift.__send__(self, *args) }
>>   end
>> end
>> end
>>
>> And it does come very handy indeed.
>> Balint
>>
>> On Feb 6, 1:07 am, Kenneth McDonald  
>> <[email protected]>
>> wrote:
>>> I've seen a claim on the web that &:f is just Ruby shorthand for
>>> &proc
>>> { |i| i.f }, but I've certainly never been able to get this &:f
>>> notation to work in standard ruby.
>>>
>>> Thanks,
>>> Ken
>>>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to