From: Michael Garriss Date: 2003-08-01T07:13:31+09:00 Subject: Re: Extracting a parent class It is VERY possible that I should be doing it in another way. My motivation: I have a client/server arch. The server maintains objects and a database. When the client needs something I do not want the client to have full access to all the info found in those server side objects. Some of it would be bad for the client to know. I have classes the client side can use and I add the additional things the server knows about the object by creating a subclass of the client class and then just adding the one or two 'secret' members. Does this make sense? Hal E. Fulton wrote: >----- Original Message ----- >From: "Michael Garriss" >To: "ruby-talk ML" >Sent: Thursday, July 31, 2003 4:53 PM >Subject: Extracting a parent class > > > > >>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? >> >> > >I don't know of a simple, direct way to do this. > >Offhand it's possible that you're thinking in a >C++ fashion and should simply do it another way. > >You *could* add a class method for B that would >create an instance of A and return it... > > class B > def B.to_A > x = A.new > x.a = self.a > x > end > end > >That's consistent with the ordinary principles of >an object knowing how to convert itself and a child >class having knowledge of a parent but not vice versa. > >Hal > > >-- >Hal Fulton >hal9000@hypermetrics.com > > > > >