28

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.

Joshua Ball
  • 23,260
  • 6
  • 27
  • 29

3 Answers3

22

lambdas 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      # => Proc
lambda{|x| x}.call(123)  # => 123

class Foo
  def bar(baz)
    baz
  end
end

puts Foo.new.method(:bar).class     # => Method
puts Foo.new.method(:bar).call(123) # => 123

puts Foo.instance_method(:bar).class     # => UnboundMethod
puts 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.

Brian Carper
  • 71,150
  • 28
  • 166
  • 168
  • Methods are not objects. The `Object#method` returns a `Method`, but othewise a method is not a `Method`. https://stackoverflow.com/questions/2602340/methods-in-ruby-objects-or-not – Nathan Hinchey May 25 '20 at 16:24
5

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

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Yes, proc's and lambdas are both Proc objects, but what do they mean by "are functions rather than methods." And what is the distinction? – Joshua Ball May 29 '09 at 22:48
  • He means the sentence in the middle of the 6th paragraph is inaccurate. – Ian Terrell May 30 '09 at 01:02
  • 1
    The difference is really that, almost universally, procs and lambdas are only used for their associated 'call' method. No one stores additional methods or values with them. So, like Kaptajn Kold says, we can use them as we would use first order functions in other languages, even though they are still really objects. – rampion May 30 '09 at 03:37
  • So in a sense Procs and lambdas are technically "methods" because everything in Ruby is an object, but in practice they behave like "functions" would in other languages—they are anonymous bits of code that aren't bound to a specific object (other than the Proc class)? – Lee McAlilly Jun 05 '20 at 17:05
5

I think the distinction is between methods and first order function ie. functions that can be passed around as values.

KaptajnKold
  • 10,638
  • 10
  • 41
  • 56