[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is the fastest way to convert a object?

Magicloud Magiclouds

7/17/2007 5:56:00 AM

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.

13 Answers

Ryan Davis

7/17/2007 6:42:00 AM

0


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.


Robert Klemme

7/17/2007 7:08:00 AM

0

2007/7/17, Ryan Davis <ryand-ruby@zenspider.com>:
>
> 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

Magicloud Magiclouds

7/17/2007 7:27:00 AM

0

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?

Peña, Botp

7/17/2007 7:31:00 AM

0

From: Magicloud Magiclouds [mailto:magicloud.magiclouds@gmail.com]
# 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

Magicloud Magiclouds

7/17/2007 7:31:00 AM

0

Robert Klemme wrote:
> 2007/7/17, Ryan Davis <ryand-ruby@zenspider.com>:
>>
>> 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?

Peña, Botp

7/17/2007 7:36:00 AM

0

From: Peña, Botp [mailto:botp@delmonte-phil.com]
# irb(main):007:1> def initialize
# irb(main):008:2> @var1=0
# irb(main):009:2> end

oops, ignore the init.

Magicloud Magiclouds

7/17/2007 7:45:00 AM

0

Peña wrote:
> From: Magicloud Magiclouds [mailto:magicloud.magiclouds@gmail.com]
> # 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.

Ryan Davis

7/17/2007 7:54:00 AM

0


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.


Robert Klemme

7/17/2007 7:55:00 AM

0

2007/7/17, Magicloud Magiclouds <magicloud.magiclouds@gmail.com>:
> Robert Klemme wrote:
> > 2007/7/17, Ryan Davis <ryand-ruby@zenspider.com>:
> >>
> >> 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

Magicloud Magiclouds

7/17/2007 8:25:00 AM

0

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.