Are methods objects in Ruby?

Asked

Viewed 73 times

4

Ruby’s got the class Proc, that is defined in the documentation as

Blocks of code that have been bound to a set of local variables. Once bound, the code may be called in Different contexts and still access those variables.

It is this class that enables Amble in that language.

Beyond the Proc has the class Method, which does not have a description in the documentation other than

Public Instance Methods

The question is whether the unnamed functions such as

def greet
  "hello there"
end

are also objects. If they are objects, they are of the class Proc or Method? And why they are or are not objects?

Of course, in an abstract way, they are objects. But they’re objects inside Ruby?

1 answer

4


Depending on the context any function or method is an object in any language. But ok, I understand what context you’re talking about.

I always say that people have become so obsessed with OOP that everyone has to say that language is OO. And a frequently used marketing is to say that everything is an object, and the objects need to be first class.

It is possible, but complicated, to make code an object. At least the performance will be very bad.

Ruby does not make the method to be an object, but has a representation of the method that is an object, so it is said roughly. This alone brings some disadvantages to the language, but also brings flexibility. Think about having a class of reflection that gives you the information of the method, that is, the object of the representation is an object, not the method itself.

I may be wrong, but from what I understand Method is for the declared methods and Proc is for anonymous since it is a structure of closure and this yes is an object and mandatory for the lambda mechanism. The Method is only necessary in cases of reflection or similar mechanisms.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.