What is the fastest way to convert a object?

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.

On Jul 16, 2007, at 22:56 , Magicloud M. wrote:

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.

2007/7/17, Ryan D. [email protected]:

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

Ryan D. wrote:

end
“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?

Robert K. wrote:

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

From: Magicloud M. [mailto:[email protected]]

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

Peña wrote:

irb(main):008:2> @var1=0
=> [“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.

From: Peña, Botp [mailto:[email protected]]

irb(main):007:1> def initialize

irb(main):008:2> @var1=0

irb(main):009:2> end

oops, ignore the init.

On Jul 17, 2007, at 00:31 , Magicloud M. 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 D. wrote:

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.

Robert K. wrote:

def initialize

How to do this quickly?

I am using DRb, and I have many data models need to be generated on

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.

2007/7/17, Magicloud M. [email protected]:

end
obj.methodA

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 K. wrote:

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?

2007/7/17, Magicloud M. [email protected]:

@var1 = 0

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