From: Joel VanderWerf Date: 2008-04-12T15:06:45+09:00 Subject: Re: How to duplicate a object changing the class without attributes memory copy I単aki Baz Castillo wrote: > Hi, I'm doing a server and want to avoid memory innecesary operations. > I have an array of headers: > headers_array > > Each header is an object of "Header" class: > > class Header > attr_accessor :name, :body > def initialize(name, body) > @name = name > @body = body > end > end > > > There are also others specific headers soons of "Header" class: > > class From < Header > attr_accessor :uri > end > > > At the beggining "headers_array" just contain "Header" objects but > after inspection I want to replace the header with @name='From' with a > "From" object but since "name" and "body" attributes are the same I > don't want a memory copy operation to allocate space for new object. > > This is: I just want to change the Class of a "Header" object to > "From" class, keeping the attributes without memory copy operation, is > there any way? I imagine in C I'd do it by using pointers. Why do you need a From class that is distinct from Header? 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 -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407