In the Ruby Programming Language, Chapter 6 (second paragraph) they state:
Many languages distinguish between functions, which have no associated object, and methods, which are invoked on a receiver object. Because Ruby is a purely object oriented language, all methods are true methods and are associated with at least one object.
And then in the middle of the 6th paragraph:
Both procs and lambdas are functions rather than methods invoked on an object.
I am a bit confused about these statements. Is Ruby truly pure OO, and therefore doesn't have functions that aren't associated with objects (if that is a valid definition of pure OO), or are procs/lambdas associated with the Proc object? What is the difference between functions and methods in Ruby?
Any help in parsing and understanding this would be appreciated.
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
orUnboundMethod
, 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.You can
bind
anUnboundMethod
to an object and then call it. But you can'tbind
aProc
to an object at all.Proc
objects can however capture local variables in the surrounding scope, becoming closures.Procs and lambdas are both objects unto themselves, with a
call
method that actually invokes the block associated with the proc (or lambda). However, Ruby provides some syntactic sugar to invoke them without the explicit call tocall
.I think the distinction is between methods and first order function ie. functions that can be passed around as values.