Say I have the following classes:
class Foo
attr_accessor :name, :age
end
class Bar
def initialize(name)
@foo = Foo.new
@foo.name = name
end
end
I'd like to define an accessor on Bar that is simply an alias to foo.name. Easy enough:
def name
@foo.name
end
def name=(value)
@foo.name = value
end
With only one property, this is easy enough. However, say Foo exposes several properties that I want to expose through Bar. Rather than defining each method manually, I want to do something like this, though I know the syntax isn't right:
[:name, :age].each do |method|
def method
@foo.method
end
def method=(value)
@foo.method = value
end
end
So...what is the correct way of defining methods like this?