Ruby functions vs methods
lambda
s in Ruby are objects of class Proc. Proc
objects don't belong to any object. They are called without binding them to an object.
Methods are objects of either class Method
or UnboundMethod
, depending on whether they're bound or unbound. See the explanation here. Unbound methods can't be called until they're bound to an object.
lambda{|x| x}.class # => Proclambda{|x| x}.call(123) # => 123class Foo def bar(baz) baz endendputs Foo.new.method(:bar).class # => Methodputs Foo.new.method(:bar).call(123) # => 123puts Foo.instance_method(:bar).class # => UnboundMethodputs Foo.instance_method(:bar).call(123) # => throws an exception
You can bind
an UnboundMethod
to an object and then call it. But you can't bind
a Proc
to an object at all. Proc
objects can however capture local variables in the surrounding scope, becoming closures.
I think the distinction is between methods and first order function ie. functions that can be passed around as values.