Variable or method not defined in Ruby How do I solve it with metaprogramming?

1

Good that such, I am tried to define a behavior to the Person Class:

What I need to do is that by placing the "transactional" within the body of the class dynamically I include a mixin to the class or execute some method. I understand that it can be done with metaprogramming but I do not know how. I hope you can give me some help. Thanks in advance.

    
asked by Matias 04.11.2017 в 18:12
source

1 answer

1

I do not know exactly what behavior you are looking for or why you want to use metaprogramming , but considering that you have a Transaccional module:

module Transaccional
  # métodos
end

You could do it by adding the transaccional method to the class Class , which is responsible for making include ; for example:

class Class
  def transaccional
    self.class_eval("include Transaccional")
  end
end

Now using transaccional within the body of the class will be the same as using include Transaccional .

If you only want to add the method dynamically and without metaprogramming then the only thing you should do is this:

Persona.include(Transaccional)

In this case you will not need to put anything in the body of the class Persona :

class Persona
  include Lockeable

  attr_accesor :nombre, :edad
  # ...
end
    
answered by 04.11.2017 / 19:46
source