From: "news.sympatico.qc.ca" Date: 2001-08-24T11:50:15+09:00 Subject: [ruby-talk:20239] Re: Inherit singleton method? First, thank you for your prompt response The problem is that I would have a copy of the value (attributes) as it was when 'clone' was invoked. Instead of having the current state of the object duplicated, I would prefer having a new object ('initialize' method invoked, ....) Note: I know, the example is far fetch (object 'x' won't ressemble the class 'OriginalClass' anymore) : just suppose the added attributes and methods (or changed methods) respect the essence of the original class. In the theorical application of singleton methods, I don't want to consider creating another class (based on, or not based on, the Original class). # -- beginning of the example (second version) class OriginalClass attr_accessor :value def initialize(value) @value = value end def compute @value = @value * 5 end end myObj = OriginalClass.new(30) # myObj.value = 30 puts myObj.compute # Expected : 30 myObj.compute # myObj.value = 150 class << myObj def initialize(value) @initValue = value @value = value end end # --> Here : Create the object based on myObj secondObj = ???(10) # secondObj.value = 10, initValue = 10 puts secondObj.value # Expected : 10 secondObj.compute # secondObj.value = 50, initValue = 10 puts secondObj.value # Expected : 50 class << myObj def compute @value = @value + @initValue end end myObj.compute # Error: initValue not defined (suppose we could continue) puts myObj.value # Expected : 150 # --> Here : Create another object based on myObj thirdObj = ???(20) # thirObj.value = 20, initValue = 20 puts thirdObj.value # Expected : 20 thirdObj.compute # thirdObj.value = 40 puts thirdObj.value # Expected : 40 myObj.compute # Error: initValue not defined (suppose we could continue) puts myObj.value # Expected : 150 secondObj.compute # secondObj.value = 60, initValue = 10 puts secondObj.value # Expected : 60 # -- end of example Finally, would it be possible to CREATE a CLASS based on the actual methods/attributes of an object? At first glance, I don't think it's possible... Avi Bryant a �crit dans le message ... >On Fri, 24 Aug 2001, Marco Guay wrote: > >> I would like to create, just by curiosity (not a real-life situation), a new >> object (like 'otherObj') based on OriginalClass, but with the specific >> definition of 'x' (new method 'doSomething' included). By intuition, I >> could do: >> otherObject = x.new # not very interesting >> or >> otherObject = x.class.new # if class could be the modified local copy >> of the class (if I can say that) > >How about: > otherObject = x.clone > >Avi > >