class Example
attr_accessor :a, :b
def initialize(a,b)
@a = a
@b = b
end
end
@ex = Example.new("A", "B")
@ex.a # =>"A"
attr_accessor is a method that defines the setter and getter methods of instance variables that match the argument symbols. In this case, "a" is the getter and "= a" is the setter. In other words, attr_accessor: a created the methods "a" and "= a".
@ex.methods.grep /a/ # => [:a.:a=,...]
Recommended Posts