4
It is very common in Ruby to use methods within classes, such as attr_accessor
, or even libraries, such as validates_presence_of
, ruby on Rails.
I would like to create:
class Person
add_name_getter "Luiz"
end
To add a field name
class, with the value passed, as "Luiz"
, in the above example.
p = Person.new
p.name == "Luiz" # true
I know it’s a useless thing, but it’s just for example.
I tried to create a method like this:
def add_name_getter *args
puts args.to_s
end
However, I did not receive any instance or class reference as a parameter.
Faced with this, remain the questions:
- What they call these constructions?
- What exactly are these constructions that are used in the classes (I referred to them as "methods", although I don’t know if they are exactly methods);
- How can I create such a building?