Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby functions vs methods

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.

like image 953
Joshua Ball Avatar asked May 29 '09 22:05

Joshua Ball


People also ask

Are Ruby methods functions?

Ruby methods are very similar to functions in any other programming language.

What is the difference between a function and a method?

A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.

Should I use method or function?

Here's a simple rule of thumb: if the code acts upon a single instance of an object, use a method. Even better: use a method unless there is a compelling reason to write it as a function. Don't over think it.

What are functions in Ruby?

Functions in Ruby are created using the def keyword (short for define). Functions that exist in an object are typically called methods. Functions and methods are the same, except one belongs to an object. Objects are created from classes using the .new method.


2 Answers

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.

like image 106
Brian Carper Avatar answered Sep 20 '22 09:09

Brian Carper


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.

like image 24
mipadi Avatar answered Sep 20 '22 09:09

mipadi