From: Robert Klemme Date: 2008-04-14T22:00:23+09:00 Subject: Re: How to duplicate a object changing the class without attributes memory copy 2008/4/12, Iņaki Baz Castillo : > 2008/4/12, Joel VanderWerf : > > > > Why do you need a From class that is distinct from Header? > > > Because in an initial parsing I just get a headers array, whiotu > inspecting each of them. > Later, if I need, I look for some specific headers (From, To...) and > need to add some methods to them. IMHO it is more efficient and less error prone if you create appropriate instances during initial parsing. You have all the information at that point in time plus you you save one additional pass through the collection. > > Depending on the > > reason, it might work to make From a module: > > > > module From > > # special From methods here > > end > > > > headers.each do |h| > > if h.name == "From" > > h.extend From > > end > > # or maybe something like: > > # h.extend const_get(h.name) > > end > > > Thanks, this is great! I didn't know "extend" method. Well, you can even do it during construction: class Header attr_accessor :name, :body def initialize(name, body) @name = name @body = body extend From if /From/ =~ name end end But I'd rather leave the decision to the parsing code. Cheers robert -- use.inject do |as, often| as.you_can - without end