From: Paul Brannan Date: 2003-02-20T00:26:57+09:00 Subject: Re: How to test for existence of instance variable? On Thu, Feb 20, 2003 at 12:02:21AM +0900, Brian Candler wrote: > #!/usr/local/bin/ruby -w > mystruct = Struct.new("Mystruct","a","b","c") > obj = mystruct.new > > mystruct.instance_eval { attr_accessor :d } # add a new member I don't think this is a good idea, since d isn't recognized as a member: irb(main):001:0> mystruct = Struct.new("Mystruct","a","b","c") => Struct::Mystruct irb(main):002:0> obj = mystruct.new => # irb(main):003:0> mystruct.instance_eval { attr_accessor :d } => nil irb(main):004:0> obj.members => ["a", "b", "c"] It would be better, I think, to inherit from Struct and write a new method to dynamically add a member. You can then define members() in the derived class to include "d" as a member, and you can add a new method to easily lookup whether a given member is already defined. Paul