Are methods objects in Ruby?

4

Ruby has the class Proc , which 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.

This class enables lambdas in this language.

In addition to Proc has the class Method , which has no descriptive in the documentation in addition to

  

Public Instance Methods

The question is whether non-anonymous functions, such as

def greet
  "hello there"
end

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

, in an abstract way, are objects . But are they objects within Ruby?

    

asked by anonymous 01.12.2017 / 13:53

1 answer

4

Depending on the context any function or method is an object in any language. But okay, I understood 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 marketing is often used to say that everything is an object, and objects must be first class .

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

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

I may be wrong, but as far as I understand Method is for declared methods and Proc is for anonymous since it is a structure of closure and this yes is an obligatory object for the lambda to run the mecansimo. Method is only required in cases of reflection or similar mechanisms.

    
01.12.2017 / 14:28