I have written a class in ruby and I wish to define the method [] to access its members (all members are public). For example, for the following simple class:
class Boy
attr_accessor :name, :age
def initialize(n, a)
@name = n
@age = a
end
end
I wish the following to be the same
b = Boy.new(Tom, 23)
b.name # Tom
b[:name] # Tom
Any help please? Thanks in advance
Or
And if you want to define []= method,
For the first style:
For the second style: