Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What is the fastest way to convert a object?

Reply
Thread Tools

What is the fastest way to convert a object?

 
 
Magicloud Magiclouds
Guest
Posts: n/a
 
      07-17-2007
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.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      07-17-2007

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.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-17-2007
2007/7/17, Ryan Davis <ryand->:
>
> 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

 
Reply With Quote
 
Magicloud Magiclouds
Guest
Posts: n/a
 
      07-17-2007
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?

 
Reply With Quote
 
Peņa, Botp
Guest
Posts: n/a
 
      07-17-2007
From: Magicloud Magiclouds [private.php?do=newpm&u=]=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

 
Reply With Quote
 
Magicloud Magiclouds
Guest
Posts: n/a
 
      07-17-2007
Robert Klemme wrote:
> 2007/7/17, Ryan Davis <ryand->:
>>
>> 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?

 
Reply With Quote
 
Peņa, Botp
Guest
Posts: n/a
 
      07-17-2007
From: Pe=F1a, Botp [private.php?do=newpm&u=]=20
# irb(main):007:1> def initialize
# irb(main):008:2> @var1=3D0
# irb(main):009:2> end

oops, ignore the init.=20

 
Reply With Quote
 
Magicloud Magiclouds
Guest
Posts: n/a
 
      07-17-2007
Peņa wrote:
> From: Magicloud Magiclouds [private.php?do=newpm&u=]
> # 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.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      07-17-2007

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.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-17-2007
2007/7/17, Magicloud Magiclouds <>:
> Robert Klemme wrote:
> > 2007/7/17, Ryan Davis <ryand->:
> >>
> >> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The fastest way to convert a long list of date loredana.pier@gmail.com Python 3 02-08-2009 04:30 PM
what would be the easiest/fastest way to convert Hash keys Jarmo Pertman Ruby 0 01-07-2009 07:51 AM
Fastest way to convert sql result into a dict or list ? rewonka@gmail.com Python 4 10-30-2008 08:24 AM
Fastest way to convert a byte of integer into a list Godzilla Python 15 07-15-2007 02:37 AM
Fastest 5 mp Digital Camera ? Fastest 4 mp Digital Camera? photoguysept102004@yahoo.com Digital Photography 6 10-28-2004 11:33 AM



Advertisments
 



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