logo       

Re: Modifying Struct members: msg#02395

ruby-talk

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

Google Custom Search

News | Mail Home | sitemap | FAQ | advertise