What is the fastest way to convert a object?

Discussion in 'Ruby' started by Magicloud Magiclouds, Jul 17, 2007.

  1. Dear all,


    Class Test
    def methodA
    puts(@var1)
    end
    def initialize
    @var1 = 0
    end
    end

    obj = Object.new
    obj.instance_variable_set:)@var1, 2)


    # Now I want obj to be Test's instance, so I can
    obj.methodA
    # How to do this quickly?



    Thanks.
    Magicloud Magiclouds, Jul 17, 2007
    #1
    1. Advertising

  2. Magicloud Magiclouds

    Ryan Davis Guest

    On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:

    > Class Test
    > def methodA
    > puts(@var1)
    > end
    > def initialize
    > @var1 = 0
    > end
    > end
    >
    > obj = Object.new
    > obj.instance_variable_set:)@var1, 2)
    >
    >
    > # Now I want obj to be Test's instance, so I can
    > obj.methodA
    > # How to do this quickly?


    You don't. Ruby is strongly typed, which means that things don't just
    "morph" into other types of things automatically or manually.
    Specifically, you can't (without voodoo) set an instance's class.
    Ryan Davis, Jul 17, 2007
    #2
    1. Advertising

  3. 2007/7/17, Ryan Davis <>:
    >
    > On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    >
    > > Class Test
    > > def methodA
    > > puts(@var1)
    > > end
    > > def initialize
    > > @var1 = 0
    > > end
    > > end
    > >
    > > obj = Object.new
    > > obj.instance_variable_set:)@var1, 2)
    > >
    > >
    > > # Now I want obj to be Test's instance, so I can
    > > obj.methodA
    > > # How to do this quickly?

    >
    > You don't. Ruby is strongly typed, which means that things don't just
    > "morph" into other types of things automatically or manually.
    > Specifically, you can't (without voodoo) set an instance's class.


    Adding to that: MM, what problem are you trying to solve?

    robert
    Robert Klemme, Jul 17, 2007
    #3
  4. Ryan Davis wrote:
    >
    > On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    >
    >> Class Test
    >> def methodA
    >> puts(@var1)
    >> end
    >> def initialize
    >> @var1 = 0
    >> end
    >> end
    >>
    >> obj = Object.new
    >> obj.instance_variable_set:)@var1, 2)
    >>
    >>
    >> # Now I want obj to be Test's instance, so I can
    >> obj.methodA
    >> # How to do this quickly?

    >
    > You don't. Ruby is strongly typed, which means that things don't just
    > "morph" into other types of things automatically or manually.
    > Specifically, you can't (without voodoo) set an instance's class.
    >
    >
    >

    Would obj.extend(Test) work?
    Magicloud Magiclouds, Jul 17, 2007
    #4
  5. Magicloud Magiclouds

    Peña, Botp Guest

    From: Magicloud Magiclouds [mailto:]=20
    # Would obj.extend(Test) work?

    ah, module (instead of class) would be good for morhphing (mixin in =
    ruby's parlance)

    irb(main):003:0> module Test
    irb(main):004:1> def methodA
    irb(main):005:2> puts @var1
    irb(main):006:2> end
    irb(main):007:1> def initialize
    irb(main):008:2> @var1=3D0
    irb(main):009:2> end
    irb(main):010:1> end
    =3D> nil
    irb(main):011:0> obj=3DObject.new
    =3D> #<Object:0xb7d5dab0>
    irb(main):012:0> obj.instance_variable_set:)@var1,2)
    =3D> 2
    irb(main):013:0> obj.extend Test
    =3D> #<Object:0xb7d5dab0 @var1=3D2>
    irb(main):021:0> obj.methods.grep /^method/
    =3D> ["method", "methodA", "methods"]
    irb(main):022:0> obj.methodA
    2
    =3D> nil

    kind regards -botp
    Peña, Botp, Jul 17, 2007
    #5
  6. Robert Klemme wrote:
    > 2007/7/17, Ryan Davis <>:
    >>
    >> On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    >>
    >> > Class Test
    >> > def methodA
    >> > puts(@var1)
    >> > end
    >> > def initialize
    >> > @var1 = 0
    >> > end
    >> > end
    >> >
    >> > obj = Object.new
    >> > obj.instance_variable_set:)@var1, 2)
    >> >
    >> >
    >> > # Now I want obj to be Test's instance, so I can
    >> > obj.methodA
    >> > # How to do this quickly?

    >>
    >> You don't. Ruby is strongly typed, which means that things don't just
    >> "morph" into other types of things automatically or manually.
    >> Specifically, you can't (without voodoo) set an instance's class.

    >
    > Adding to that: MM, what problem are you trying to solve?
    >
    > robert
    >
    >

    I am using DRb, and I have many data models need to be generated on
    server and sent to client. So I think a smaller object would be better.
    Right?
    Magicloud Magiclouds, Jul 17, 2007
    #6
  7. Magicloud Magiclouds

    Peña, Botp Guest

    From: Pe=F1a, Botp [mailto:]=20
    # irb(main):007:1> def initialize
    # irb(main):008:2> @var1=3D0
    # irb(main):009:2> end

    oops, ignore the init.=20
    Peña, Botp, Jul 17, 2007
    #7
  8. Peña wrote:
    > From: Magicloud Magiclouds [mailto:]
    > # Would obj.extend(Test) work?
    >
    > ah, module (instead of class) would be good for morhphing (mixin in ruby's parlance)
    >
    > irb(main):003:0> module Test
    > irb(main):004:1> def methodA
    > irb(main):005:2> puts @var1
    > irb(main):006:2> end
    > irb(main):007:1> def initialize
    > irb(main):008:2> @var1=0
    > irb(main):009:2> end
    > irb(main):010:1> end
    > => nil
    > irb(main):011:0> obj=Object.new
    > => #<Object:0xb7d5dab0>
    > irb(main):012:0> obj.instance_variable_set:)@var1,2)
    > => 2
    > irb(main):013:0> obj.extend Test
    > => #<Object:0xb7d5dab0 @var1=2>
    > irb(main):021:0> obj.methods.grep /^method/
    > => ["method", "methodA", "methods"]
    > irb(main):022:0> obj.methodA
    > 2
    > => nil
    >
    > kind regards -botp
    >
    >

    Yes, that is good. Thanks.
    I need to try it to see if it is right for me.
    Magicloud Magiclouds, Jul 17, 2007
    #8
  9. Magicloud Magiclouds

    Ryan Davis Guest

    On Jul 17, 2007, at 00:31 , Magicloud Magiclouds wrote:

    > I am using DRb, and I have many data models need to be generated on
    > server and sent to client. So I think a smaller object would be
    > better. Right?


    Wrong. Just do what you need to do in the simplest implementation
    possible. Worry about size/bandwidth/other when it actually becomes a
    problem. Chances are, it won't. For all you know, you could have been
    done by now.

    Wait...

    What makes you think that Object.new is smaller than Test.new? No.
    Just write your code for correctness and stop optimizing stuff you
    aren't measuring in the first place. You'll be happier.
    Ryan Davis, Jul 17, 2007
    #9
  10. 2007/7/17, Magicloud Magiclouds <>:
    > Robert Klemme wrote:
    > > 2007/7/17, Ryan Davis <>:
    > >>
    > >> On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    > >>
    > >> > Class Test
    > >> > def methodA
    > >> > puts(@var1)
    > >> > end
    > >> > def initialize
    > >> > @var1 = 0
    > >> > end
    > >> > end
    > >> >
    > >> > obj = Object.new
    > >> > obj.instance_variable_set:)@var1, 2)
    > >> >
    > >> >
    > >> > # Now I want obj to be Test's instance, so I can
    > >> > obj.methodA
    > >> > # How to do this quickly?
    > >>
    > >> You don't. Ruby is strongly typed, which means that things don't just
    > >> "morph" into other types of things automatically or manually.
    > >> Specifically, you can't (without voodoo) set an instance's class.

    > >
    > > Adding to that: MM, what problem are you trying to solve?
    > >
    > > robert
    > >
    > >

    > I am using DRb, and I have many data models need to be generated on
    > server and sent to client. So I think a smaller object would be better.
    > Right?


    Maybe, maybe not. Here are my 0.02 EUR:

    1. I'd verify that "large" objects are really an issue
    2. I'd make conversion explicit by means ot a #to_xyz method, i.e.
    receive an object and invoke obj.to_your_real_stuff on it.

    Kind regards

    robert
    Robert Klemme, Jul 17, 2007
    #10
  11. Ryan Davis wrote:
    >
    > On Jul 17, 2007, at 00:31 , Magicloud Magiclouds wrote:
    >
    >> I am using DRb, and I have many data models need to be generated on
    >> server and sent to client. So I think a smaller object would be
    >> better. Right?

    >
    > Wrong. Just do what you need to do in the simplest implementation
    > possible. Worry about size/bandwidth/other when it actually becomes a
    > problem. Chances are, it won't. For all you know, you could have been
    > done by now.
    >
    > Wait...
    >
    > What makes you think that Object.new is smaller than Test.new? No. Just
    > write your code for correctness and stop optimizing stuff you aren't
    > measuring in the first place. You'll be happier.
    >
    >
    >

    Yes, thank you.
    Magicloud Magiclouds, Jul 17, 2007
    #11
  12. Robert Klemme wrote:
    > 2007/7/17, Magicloud Magiclouds <>:
    >> Robert Klemme wrote:
    >> > 2007/7/17, Ryan Davis <>:
    >> >>
    >> >> On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    >> >>
    >> >> > Class Test
    >> >> > def methodA
    >> >> > puts(@var1)
    >> >> > end
    >> >> > def initialize
    >> >> > @var1 = 0
    >> >> > end
    >> >> > end
    >> >> >
    >> >> > obj = Object.new
    >> >> > obj.instance_variable_set:)@var1, 2)
    >> >> >
    >> >> >
    >> >> > # Now I want obj to be Test's instance, so I can
    >> >> > obj.methodA
    >> >> > # How to do this quickly?
    >> >>
    >> >> You don't. Ruby is strongly typed, which means that things don't just
    >> >> "morph" into other types of things automatically or manually.
    >> >> Specifically, you can't (without voodoo) set an instance's class.
    >> >
    >> > Adding to that: MM, what problem are you trying to solve?
    >> >
    >> > robert
    >> >
    >> >

    >> I am using DRb, and I have many data models need to be generated on
    >> server and sent to client. So I think a smaller object would be better.
    >> Right?

    >
    > Maybe, maybe not. Here are my 0.02 EUR:
    >
    > 1. I'd verify that "large" objects are really an issue
    > 2. I'd make conversion explicit by means ot a #to_xyz method, i.e.
    > receive an object and invoke obj.to_your_real_stuff on it.
    >
    > Kind regards
    >
    > robert
    >
    >

    Yes, this is my problem, too.
    I need an efficient way to transfer data model, but it seems that drb is
    not just "transfer".
    Even if I ignore the object size thing, still, calling a drbObject's
    complex method is an issue.
    Magicloud Magiclouds, Jul 17, 2007
    #12
  13. 2007/7/17, Magicloud Magiclouds <>:
    > Robert Klemme wrote:
    > > 2007/7/17, Magicloud Magiclouds <>:
    > >> Robert Klemme wrote:
    > >> > 2007/7/17, Ryan Davis <>:
    > >> >>
    > >> >> On Jul 16, 2007, at 22:56 , Magicloud Magiclouds wrote:
    > >> >>
    > >> >> > Class Test
    > >> >> > def methodA
    > >> >> > puts(@var1)
    > >> >> > end
    > >> >> > def initialize
    > >> >> > @var1 = 0
    > >> >> > end
    > >> >> > end
    > >> >> >
    > >> >> > obj = Object.new
    > >> >> > obj.instance_variable_set:)@var1, 2)
    > >> >> >
    > >> >> >
    > >> >> > # Now I want obj to be Test's instance, so I can
    > >> >> > obj.methodA
    > >> >> > # How to do this quickly?
    > >> >>
    > >> >> You don't. Ruby is strongly typed, which means that things don't just
    > >> >> "morph" into other types of things automatically or manually.
    > >> >> Specifically, you can't (without voodoo) set an instance's class.
    > >> >
    > >> > Adding to that: MM, what problem are you trying to solve?
    > >> >
    > >> > robert
    > >> >
    > >> >
    > >> I am using DRb, and I have many data models need to be generated on
    > >> server and sent to client. So I think a smaller object would be better.
    > >> Right?

    > >
    > > Maybe, maybe not. Here are my 0.02 EUR:
    > >
    > > 1. I'd verify that "large" objects are really an issue
    > > 2. I'd make conversion explicit by means ot a #to_xyz method, i.e.
    > > receive an object and invoke obj.to_your_real_stuff on it.
    > >
    > > Kind regards
    > >
    > > robert
    > >
    > >

    > Yes, this is my problem, too.
    > I need an efficient way to transfer data model, but it seems that drb is
    > not just "transfer".
    > Even if I ignore the object size thing, still, calling a drbObject's
    > complex method is an issue.


    First thing you should make sure is that you have a clear
    understanding of your object model and which of those objects are
    remote and which are not. That distinction is important for all
    distributed applications regardless of technology used since usually
    network communication is the most expensive part of distribution at
    runtime. Although I agree to not prematurely optimize I think for a
    distributed application the design of the remote interfaces is
    important, especially the granularity of method calls and data sent
    around.

    Kind regards

    robert
    Robert Klemme, Jul 17, 2007
    #13
  14. Robert Klemme wrote:
    >> Yes, this is my problem, too.
    >> I need an efficient way to transfer data model, but it seems that drb is
    >> not just "transfer".
    >> Even if I ignore the object size thing, still, calling a drbObject's
    >> complex method is an issue.

    >
    > First thing you should make sure is that you have a clear
    > understanding of your object model and which of those objects are
    > remote and which are not. That distinction is important for all
    > distributed applications regardless of technology used since usually
    > network communication is the most expensive part of distribution at
    > runtime. Although I agree to not prematurely optimize I think for a
    > distributed application the design of the remote interfaces is
    > important, especially the granularity of method calls and data sent
    > around.
    >
    > Kind regards
    >
    > robert
    >
    >

    Yes, I think so, the design is important, not optimize. I am using some
    testing to see how drb works, but something is hard to detect. Could you
    tell me your conclusion till now?
    Magicloud Magiclouds, Jul 17, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Godzilla
    Replies:
    15
    Views:
    503
    jigloo
    Jul 15, 2007
  2. caultonpos
    Replies:
    3
    Views:
    471
    ownowl
    Jun 19, 2008
  3. Replies:
    4
    Views:
    678
    Peter Otten
    Oct 30, 2008
  4. Replies:
    3
    Views:
    411
  5. Jarmo Pertman
    Replies:
    0
    Views:
    106
    Jarmo Pertman
    Jan 7, 2009
Loading...

Share This Page