[LRUG] Proc.new and &
Damian Janowski
damian.janowski at gmail.com
Sun Sep 16 11:47:40 PDT 2007
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
More information about the Chat
mailing list