[LRUG] Proc.new and &

Daniel Tenner daniel.ruby at tenner.org
Sun Sep 16 11:25:40 PDT 2007


Hi all,

I'm defining a validator that works when either one field or the  
other is set. This is what it looks like at the moment:

   validates_presence_of :b, :if => Proc.new { |user| !user.a }
   validates_presence_of :a, :if => Proc.new { |user| !user.b }

This works, but I'd like to somehow be able to use the & shortcut  
here, like this:

   validates_presence_of :b, :if => !Proc.new(&:a)
   validates_presence_of :a, :if => !Proc.new(&:b)

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...

Thanks for any suggestions!

Daniel



More information about the Chat mailing list