From: Geoff Youngs Date: 2003-08-01T17:33:02+09:00 Subject: Re: Extracting a parent class On Fri, Aug 01, 2003 at 07:12:19AM +0900, 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. Unless you were to use the Struct class instead of a normal class, in which case you can tell what should stay and what should go. e.g. class Struct def cast_as(klass) raise "Not a Class" unless klass.kind_of?(Class) raise "Not a Struct" unless klass.ancestors.include?(Struct) obj = klass.new() members.each do |name| obj[name] = self[name] if obj.members.include?(name) end obj end end StructA = Struct.new('StructA',:alpha, :beta) StructB = Struct.new('StructB',:alpha, :beta, :gamma) foo = Struct::StructB.new(1,2,3) bar = foo.cast_as(Struct::StructA) p foo,bar With a bit more effort you could try a reverse castable object - ie. one which would preserve members unused by the target class and copy all instance variables, but I don't think that that is what is required here. HTH, Geoff