From: Michael Garriss Date: 2003-08-01T11:34:34+09:00 Subject: Re: Extracting a parent class Just in case anyone is following this, I have a new solution to my problem. It only works if each instance variable in the parent class is accessable. Anyone who can solve this or has a hint...plz tell me! Here's the test code: module SuperCastable def to_super super_obj = self.class.superclass.new instance_variables.each { |var| begin eval "super_obj.#{var[1..-1]} = #{var}" rescue NoMethodError end } super_obj end end class A attr_accessor :a end class B < A include SuperCastable attr_accessor :b end b = B.new b.a = "some data" b.b = "some more data" a = b.to_super Daniel Carrera wrote: >Oh, sorry. >In that case I don't imagine it's possible by default because in general >there might not be an "obvious" way to turn class B into class A. > >You should make a B#to_A method. > >Cheers, >Daniel. > >On Fri, Aug 01, 2003 at 07:09:07AM +0900, Michael Garriss wrote: > > >>Sorry...I should have been more clear. While this works it does not >>mantain the state. I would like all the members of 'b' that are class >>A members to be set in 'a'. >> >>Daniel Carrera wrote: >> >> >> >>>Answer: >>>a = b.class.superclass.new >>> >>>Why: >>>b.class == B >>>b.class.superclass == A >>>b.class.superclass.new == A.new >>> >>>Cheers, >>>Daniel. >>> >>>On Fri, Aug 01, 2003 at 06:53:01AM +0900, Michael Garriss wrote: >>> >>> >>> >>> >>>>Sorry about the newbie question but.... >>>> >>>>class A >>>>attr_accessor :a >>>>end >>>> >>>>class B < A >>>>attr_accessor :b >>>>end >>>> >>>>b = B.new >>>>b.a = "some data" >>>>b.b = "some more data" >>>>a = b.????? >>>> >>>>How do I set 'a' from 'b' so that a is of class A and not class B? I >>>>don't want the extra info, I want just the subset of members found in >>>>the parent class. Is there a way to cast? >>>> >>>>TIA >>>>Michael Garriss >>>> >>>> >>>> >>>> >>>> >>> >>> >>> >> >> > > >