I have a class like this
class Foo
attr_accessor :name
end
f = Foo.new
f.name = "bar"
and I would like it to respond to the following method chain with the name attribute so that it interfaces with another object
f.baz.name == f.name
Is there an easy way to return this?
#!/usr/bin/ruby1.8
class Foo
attr_accessor :name
def baz
self
end
end
foo = Foo.new
foo.name = 'Fred'
p [foo.name, foo.baz.name] # => ["Fred", "Fred"]
foo.baz.name = 'Barney'
p [foo.name, foo.baz.name] # => ["Barney", "Barney"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.