Professional Web Applications Themes

Extracting a parent class - Ruby

----- Original Message ----- From: "Michael Garriss" <mgarrissearthlink.net> To: "ruby-talk ML" <ruby-talkruby-lang.org> 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 ...

  1. #1

    Default Re: Extracting a parent class

    ----- Original Message -----
    From: "Michael Garriss" <mgarrissearthlink.net>
    To: "ruby-talk ML" <ruby-talkruby-lang.org>
    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
    [email]hal9000hypermetrics.com[/email]


    Hal E. Fulton Guest

  2. #2

    Default Re: Extracting a parent class

    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
    >
    --
    Daniel Carrera | PGP: 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88
    Math PhD. UMD | [url]http://www.math.umd.edu/~dcarrera/pgp.html[/url]

    * * * * * Weekly Smile * * * * * * * * * * * * * * * * * * * * * * * *
    Sign in a hotel in Athens:
    Visitors are expected to complain at the office between the hours
    of 9 and 11 A.M. daily.
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


    Daniel Carrera Guest

  3. #3

    Default Re: Extracting a parent class

    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
    >>
    >>
    >>
    >
    >
    >


    Michael Garriss Guest

  4. #4

    Default 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" <mgarrissearthlink.net>
    >To: "ruby-talk ML" <ruby-talkruby-lang.org>
    >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
    >hal9000hypermetrics.com
    >
    >
    >
    >
    >


    Michael Garriss Guest

  5. #5

    Default Re: Extracting a parent class


    --PPYy/fEw/8QCHSq3
    Content-Type: text/plain; cht=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Michael Garriss (mgarrissearthlink.net) wrote:
    > It is VERY possible that I should be doing it in another way. My=20
    > motivation: I have a client/server arch. The server maintains objects=
    =20
    > and a database. When the client needs something I do not want the=20
    > client to have full access to all the info found in those server side=20
    > objects. Some of it would be bad for the client to know. I have=20
    > classes the client side can use and I add the additional things the=20
    > server knows about the object by creating a subclass of the client class=
    =20
    > and then just adding the one or two 'secret' members. Does this make sen=
    se?

    Perhaps containment would be a better solution than inheritance?

    class ClientThingy
    attr :foo, :bar, :baz
    end

    class ServerThingy
    attr :client_thingy

    def initialize
    client_thingy =3D ClientThingy.new
    end
    end

    Also, have you looked at DRb? It will allow you to have a clean
    division between server-side and client-side.

    --=20
    Eric Hodel - [email]drbrainsegment7.net[/email] - [url]http://segment7.net[/url]
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --PPYy/fEw/8QCHSq3
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (FreeBSD)

    iD8DBQE/KZW7MypVHHlsnwQRAnDbAKDG0RrZ2NgrSTiuaH3VU+YvTfySIQ CgpeCg
    kPJfa9/FQH4EDUYA85TUMVY=
    =05Le
    -----END PGP SIGNATURE-----

    --PPYy/fEw/8QCHSq3--

    Eric Hodel Guest

  6. #6

    Default Re: Extracting a parent class

    Well, not laugh., but I am using DRb. I've had a difficult time
    figuring out it all, seems to be a lack or docs or I'm just bad at
    finding them.

    example of what I am doing:

    class ServerObject < ClientObject
    def to_client_side_version
    # blah
    end
    end

    class Server
    def get_me_an_obj
    server_obj.to_client_side_version
    end
    end

    ...

    class Client
    def ask_for_obj
    server.get_me_an_obj
    end
    end

    ....

    # this is a 3rd file that I called shared that I put on both sides
    # I know there must be another way, plz help

    class ClientObject
    end

    Eric Hodel wrote:
    >Michael Garriss (mgarrissearthlink.net) wrote:
    >
    >
    >
    >>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?
    >>
    >>
    >
    >Perhaps containment would be a better solution than inheritance?
    >
    >class ClientThingy
    > attr :foo, :bar, :baz
    >end
    >
    >class ServerThingy
    > attr :client_thingy
    >
    > def initialize
    > client_thingy = ClientThingy.new
    > end
    >end
    >
    >Also, have you looked at DRb? It will allow you to have a clean
    >division between server-side and client-side.
    >
    >
    >


    Michael Garriss Guest

  7. #7

    Default Re: Extracting a parent class

    I suppose that containment is a good idea but I do have methods that
    expect objs of the client side base class. I could rewrite them to add
    the check the class of the incoming obj. I think that I will just
    continue to use to_SomeClass type methods. It's just a pain to have to
    keep them upto date.

    Eric Hodel wrote:
    >Michael Garriss (mgarrissearthlink.net) wrote:
    >
    >
    >
    >>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?
    >>
    >>
    >
    >Perhaps containment would be a better solution than inheritance?
    >
    >class ClientThingy
    > attr :foo, :bar, :baz
    >end
    >
    >class ServerThingy
    > attr :client_thingy
    >
    > def initialize
    > client_thingy = ClientThingy.new
    > end
    >end
    >
    >Also, have you looked at DRb? It will allow you to have a clean
    >division between server-side and client-side.
    >
    >
    >


    Michael Garriss Guest

  8. #8

    Default 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
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>
    >>>
    >>
    >>
    >
    >
    >


    Michael Garriss Guest

  9. #9

    Default Re: Extracting a parent class


    --SnV5plBeK2Ge1I9g
    Content-Type: text/plain; cht=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Michael Garriss (mgarrissearthlink.net) wrote:
    > Just in case anyone is following this, I have a new solution to my=20
    > problem. It only works if each instance variable in the parent class is=
    =20
    > accessable. Anyone who can solve this or has a hint...plz tell me! =20
    You can get at anything you want using #instance_eval.

    --=20
    Eric Hodel - [email]drbrainsegment7.net[/email] - [url]http://segment7.net[/url]
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --SnV5plBeK2Ge1I9g
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (FreeBSD)

    iD8DBQE/KeRPMypVHHlsnwQRAt5QAJ0VOftIbuIJU3XwtjnxhWToBNMxKw CcCiAi
    Zk1JIqdB7lRFSgDPqGEOr/Y=
    =o6Od
    -----END PGP SIGNATURE-----

    --SnV5plBeK2Ge1I9g--

    Eric Hodel Guest

  10. #10

    Default Re: Extracting a parent class

    Very cool, thank you.

    Eric Hodel wrote:
    >Michael Garriss (mgarrissearthlink.net) wrote:
    >
    >
    >
    >>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!
    >>
    >>
    >
    >You can get at anything you want using #instance_eval.
    >
    >
    >


    Michael Garriss Guest

Similar Threads

  1. #25441 [NEW]: public properties of parent class not accessible
    By tater at potatoe dot com in forum PHP Development
    Replies: 0
    Last Post: September 8th, 10:23 PM
  2. #23038 [Com]: PHP does not detect parent class inside child class' constructor
    By hewei at ied dot org dot cn in forum PHP Development
    Replies: 0
    Last Post: August 6th, 02:58 AM
  3. Passing a method block to a parent class
    By Eric Anderson in forum Ruby
    Replies: 3
    Last Post: July 27th, 09:41 PM
  4. getting name of class and/or parent class that object belongs to
    By Jay Tilton in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 23rd, 03:46 AM
  5. HowTo get a Viewstate Value from my Parent Class?
    By Andreas Klemt in forum ASP.NET General
    Replies: 0
    Last Post: July 14th, 10:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139