From: Justin Collins Date: 2009-08-01T02:42:19+09:00 Subject: Re: Modifying Struct members Yossef Mendelssohn wrote: > Is it possible to modify the members of a Struct class once it's been > created, yet retain all other behavior? > > An example: > > Thing = Struct.new(:a, :b, :c) do > def some_method > end > end > > Is there a way to modify Thing so it behaves as if it were effectively > created like so? > > Thing = Struct.new(:a, :b, :c, :d) do > def some_method > end > end > > Re-assigning Thing to a new Struct obviously doesn't work because then > the behavior is lost. > Altering Thing.members seemed promising, but that didn't work either. > Adding attr_accessor for :d isn't good because I need Thing.new['d'] > to work. > > Thoughts? > > -- > -yossef > > OpenStruct might be what you want: http://ruby-doc.org/core/classes/OpenStruct.html -Justin