From: Phrogz Date: 2007-11-15T12:45:00+09:00 Subject: Re: adding an attribute to an object On Nov 14, 7:10 pm, Mark Volkmann wrote: > I know how to add new methods to an existing object, but is there a way to add an attribute? > I know I can do it if the object was created with ObjectStruct, but what if it is a "normal" Ruby object? As Pat pointed out, there are no 'attributes' in Ruby, just methods. (Those methods may or may not have a 1-to-1 correspondence to instance variables; that's all the convenience methods like attr_accessor do, is set up a simple method.) irb(main):001:0> o = Object.new => # irb(main):002:0> def o.duration=( seconds ) irb(main):003:1> @length = seconds irb(main):004:1> end => nil irb(main):005:0> def o.duration; @length; end => nil irb(main):006:0> o.duration = 120 => 120 irb(main):007:0> puts o.duration 120 => nil irb(main):008:0> def o.duration_in_minutes; @length / 60; end => nil irb(main):009:0> puts o.duration_in_minutes 2 => nil