logo       

Re: Modifying Struct members: msg#02398

ruby-talk

Subject: Re: Modifying Struct members

On Jul 31, 12:42 pm, Justin Collins <justincoll...@xxxxxxxx> wrote:
> OpenStruct might be what you
> want:http://ruby-doc.org/core/classes/OpenStruct.html

I appreciate the thought, but it kind of doesn't matter what I might
want. I have to stick with the crap I'm building on because I don't
feel like rewriting a gem.

In case anyone is wondering, the 'solution' (as in it works but I
don't recommend it) with help from Aaron "tenderlove" Patterson, goes
something like this.

Thing = Struct.new(:a, :b, :c) do
def some_thing
end
end

class Thing
attr_accessor :d

alias_method :old_brackets, :[]
def [](m)
if m.to_s == 'd'
self.d
else
old_brackets(m)
end
end

alias_method :old_brackets_equal, :[]=
def []=(m, v)
if m.to_s == 'd'
self.d = v
else
old_brackets_equal(m, v)
end
end
end

Tell me that doesn't make you unhappy.

--
-yossef

Google Custom Search

News | Mail Home | sitemap | FAQ | advertise