[LRUG] Proc.new and &
Daniel Tenner
daniel.ruby at tenner.org
Sun Sep 16 12:35:45 PDT 2007
Hi Damian,
Thanks for the clear explanation!
Daniel
On 16 Sep 2007, at 19:47 16 Sep 2007, Damian Janowski wrote:
> On 9/16/07, Daniel Tenner <daniel.ruby at tenner.org> wrote:
>> I've tried some variations of it, e.g. Proc.new(&!:a) (parse error),
>> Proc.new(!&:a) (same)...
>>
>> Removing the ! or putting it outside of the Proc.new appears to work,
>> but it actually doesn't do what I intend it to do.
>>
>> I'm curious whether there is a way to make this expression work with
>> & so that it returns a boolean that's negative when the field is
>> set...
>
> Hi Daniel,
>
> The ampersand only tries to convert the given object to a Proc -- ie
> calling #to_proc on the receiver. ActiveSupport implements #to_proc on
> the Symbol class, and that's why the shortcut is restricted to the
> simplest calls: &:foo is the same as {|o| o.foo }, but that's it.
> Everything becomes clearer and less magical when you understand that
> this is the same as doing (:foo).to_proc. So, currently you can't do
> what you want with plain ActiveSupport/Rails.
>
> However, you might want to take a look at Methodphitamine [1], by Jay
> Phillips, inspired by Symbol#to_proc, but way more powerful.
>
> Cheers
>
> Damian
>
> [1] http://jicksta.com/articles/2007/08/04/the-methodphitamine
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
More information about the Chat
mailing list