what is proc doing here?

Hey all,

This line of code I am having trouble understanding what it's doing:

validates_presence_of :email, :unless => proc { |this| this.validation_context == :create_user }

Basically, there is a code block that floats adjacent to a method. The method is called and returns a value. That yielded value is passed as an argument (local variable "this") into the code block and the code block returns a value to the method (in this case true or false since double equal is used and converts result into primitive (actually not but an object of type boolean) type boolean). But then I did a search for this method proc and its definition doesn't exist in application. However, a google search shows there is an object called proc. Since code blocks are not objects, instead just holding a context of logic sitting adjacent to a method which invokes them via yield, using proc is a way to convert a code block to an object. Hence, they can be stored in variables and called through the variables as a callable object. But as you see in the above example, proc is not instantiated like so: Proc.new. And even if you look at context, there will be no reason to use an instance of proc here, since we aren't storing anything into a variable for later use as a callable method, such as in this example:

hello_world = Proc.new { |str| return str + 'world' }

def fnc(new_fnc, *args) return unless new_fnc.respond_to?("call") new_fnc.call(*args) end

puts fnc hello_world, 'hey'

So my question is am I missing something here? What exactly is "proc" doing since it isn't an instance of Proc nor is it a method (there's no definition for it as I mentioned)?

Thanks for response

John Merlino wrote in post #998394:

Hey all,

This line of code I am having trouble understanding what it's doing:

validates_presence_of :email, :unless => proc { |this| this.validation_context == :create_user }

Basically, there is a code block that floats adjacent to a method. The method is called and returns a value. That yielded value is passed as an argument (local variable "this") into the code block and the code block returns a value to the method (in this case true or false since double equal is used and converts result into primitive (actually not but an object of type boolean) type boolean). But then I did a search for this method proc and its definition doesn't exist in application. However, a google search shows there is an object called proc. Since code blocks are not objects, instead just holding a context of logic sitting adjacent to a method which invokes them via yield, using proc is a way to convert a code block to an object. Hence, they can be stored in variables and called through the variables as a callable object. But as you see in the above example, proc is not instantiated like so: Proc.new. And even if you look at context, there will be no reason to use an instance of proc here, since we aren't storing anything into a variable for later use as a callable method, such as in this example:

hello_world = Proc.new { |str| return str + 'world' }

def fnc(new_fnc, *args) return unless new_fnc.respond_to?("call") new_fnc.call(*args) end

puts fnc hello_world, 'hey'

So my question is am I missing something here? What exactly is "proc" doing since it isn't an instance of Proc nor is it a method (there's no definition for it as I mentioned)?

Thanks for response

Well I have been burned for making too detailed answers, so here is the short version.

"validates_presence_of" is a method call, ":unless" is the key part of the hash part of the parameters to "validates_presence_of", and the proc (equivalent to Proc.new) part is sent as the value to key ":unless".

Hope that was short enough.

"validates_presence_of" is a method call, ":unless" is the key part of the hash part of the parameters to "validates_presence_of", and the proc (equivalent to Proc.new) part is sent as the value to key ":unless".

Hope that was short enough.

Thanks for response. I see what it's doing now.