# DRB class in array

**URL:** <https://www.ruby-forum.com/t/drb-class-in-array/179498>\
**Category:** Ruby\
**Created:** [November 26, 2009, 11:07am UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498 "2009-11-26T11:07:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [November 26, 2009, 11:07am UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/1 "2009-11-26T11:07:58Z")

</div>

hi,  
i would like to store an array of this very simple class on a drb server

class IEB\_wrapper1  
attr\_accessor :status

def initialize  
@status=“ready”  
end  
end

the object on the drb server is an array:  
dump=[]  
DRb.start\_service “druby://BLA:55555”, dump

if a client modifies the status variable of a class instance in the  
array it does not get changed.

dump = DRbObject.new nil, ARGV.shift  
myclass = IEB\_wrapper1.new  
dump.push myclass  
dump[0].status = “Hello”  
p dump[0].status =\> results in “ready” not in “Hello”

how can i modify the status of my class in the array on the drb server?

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [November 26, 2009, 11:30am UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/2 "2009-11-26T11:30:55Z")

</div>

Master M. wrote:

> if a client modifies the status variable of a class instance in the  
> array it does not get changed.
> 
> dump = DRbObject.new nil, ARGV.shift  
> myclass = IEB\_wrapper1.new  
> dump.push myclass  
> dump[0].status = “Hello”  
> p dump[0].status =\> results in “ready” not in “Hello”
> 
> how can i modify the status of my class in the array on the drb server?

class IEB\_wrapper1  
include DRbUndumped  
end

This means that instead of passing across a _copy_ of the object (made  
using Marshal.dump and load), it will pass across a DRb _proxy object_.  
All operations made on that proxy are sent across as remote method calls  
and therefore made on the server.

I wrote a document called “DrbTutorial” on [rubygarden.org](http://rubygarden.org), which  
unfortunately is long since gone. You might be able to find a cached  
copy somewhere.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [November 26, 2009, 2:06pm UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/3 "2009-11-26T14:06:54Z")

</div>

> include DRbUndumped  
> this seems to let the class reside wherever it has been generated.  
> is it possible to generate a class on a drb client, copy it to the  
> server and then let it stay there (ie. further interaction with the  
> class should be made with the the class on the server and not with local  
> copies).

what i want to do is the following:  
i have a class that has a run and a status function. when the run  
function is invoked the class computes something for ~5minutes.  
the calculation should happen on the drb server.  
multiple drb clients should generate classes on the server(or locally  
and copy them to the server), so that the server does the computation.  
after the jobs have been started (remote\_job\_array[x].start) the clients  
terminate. other clients that should check the status of the computation  
via a status function (remote\_job\_array[x].status) of the class on the  
server.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [November 26, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/4 "2009-11-26T15:09:04Z")

</div>

Master M. wrote:

> > include DRbUndumped  
> > this seems to let the class reside wherever it has been generated.  
> > is it possible to generate a class on a drb client, copy it to the  
> > server and then let it stay there (ie. further interaction with the  
> > class should be made with the the class on the server and not with local  
> > copies).

DRb has no real concept of a “server” or a “client” - connections can be  
opened and method calls made from either side.

You could originate your object, pass it across to another place, and at  
that point do  
obj.extend DRbUndumped  
which will lock it there.

But perhaps more sensible is to pass across the _parameters_ needed to  
make the object, i.e. have a class method on the server such as

def Foo.make\_status(\*args)  
IEB\_wrapper1.new(\*args)  
end

and you share the Foo object via DRb.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [December 9, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/5 "2009-12-09T15:39:43Z")

</div>

Hi

I have a slightly different problem. I have an array stored in the  
object:

class Room  
include DRb::DRbUndumped  
attr\_accessor :name  
attr\_accessor :game  
attr\_accessor :observers

def initialize(name=nil)  
@game = nil  
@observers = []  
@name = name  
end  
end

And the object is stored at the ‘server’. At the ‘client’ I got the  
object and is able to modify the @name instance variable at the  
‘server’. However when I tried to add objects to the @observers array it  
doesn’t seem to appear at the ‘server’. What could be wrong?

Brian C. wrote:

> Master M. wrote:
> 
> > if a client modifies the status variable of a class instance in the  
> > array it does not get changed.
> > 
> > dump = DRbObject.new nil, ARGV.shift  
> > myclass = IEB\_wrapper1.new  
> > dump.push myclass  
> > dump[0].status = “Hello”  
> > p dump[0].status =\> results in “ready” not in “Hello”
> > 
> > how can i modify the status of my class in the array on the drb server?
> 
> class IEB\_wrapper1  
> include DRbUndumped  
> end
> 
> This means that instead of passing across a _copy_ of the object (made  
> using Marshal.dump and load), it will pass across a DRb _proxy object_.  
> All operations made on that proxy are sent across as remote method calls  
> and therefore made on the server.
> 
> I wrote a document called “DrbTutorial” on [rubygarden.org](http://rubygarden.org), which  
> unfortunately is long since gone. You might be able to find a cached  
> copy somewhere.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [December 9, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/6 "2009-12-09T16:21:24Z")

</div>

Sausheong Chang wrote:

> I have a slightly different problem. I have an array stored in the  
> object:
> 
> class Room  
> include DRb::DRbUndumped  
> attr\_accessor :name  
> attr\_accessor :game  
> attr\_accessor :observers
> 
> def initialize(name=nil)  
> @game = nil  
> @observers = []  
> @name = name  
> end  
> end
> 
> And the object is stored at the ‘server’. At the ‘client’ I got the  
> object and is able to modify the @name instance variable at the  
> ‘server’. However when I tried to add objects to the @observers array it  
> doesn’t seem to appear at the ‘server’. What could be wrong?

Try this:

@observers = [].extend DRb::DRbUndumped

I think the same thing is happening one level down: although the Room  
instance stays where it is, when you make a call across the net to  
room.observers the return value is a _copy_ of this array.

Another solution is to remove the attr\_accessor for observers  
completely, and instead implement separate methods for what the client  
really needs to do - e.g. add an observer, remove an observer, iterate  
across observers.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [December 9, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/7 "2009-12-09T17:20:42Z")

</div>

Thanks! That worked, though a bit messier than I would have liked it.

Brian C. wrote:

> Try this:
> 
> @observers = [].extend DRb::DRbUndumped
> 
> I think the same thing is happening one level down: although the Room  
> instance stays where it is, when you make a call across the net to  
> room.observers the return value is a _copy_ of this array.
> 
> Another solution is to remove the attr\_accessor for observers  
> completely, and instead implement separate methods for what the client  
> really needs to do - e.g. add an observer, remove an observer, iterate  
> across observers.

---

<div class="post-metadata">

**Author:** ![mastermarv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mastermarv](https://www.ruby-forum.com/u/mastermarv)\
**Post date:** [November 30, 2009, 9:26am UTC](https://www.ruby-forum.com/t/drb-class-in-array/179498/8 "2009-11-30T09:26:43Z")

</div>

2009/11/26 Master M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> the calculation should happen on the drb server.  
> multiple drb clients should generate classes on the server(or locally  
> and copy them to the server), so that the server does the computation.  
> after the jobs have been started (remote\_job\_array[x].start) the clients  
> terminate. other clients that should check the status of the computation  
> via a status function (remote\_job\_array[x].status) of the class on the  
> server.

DRb does not implement the concept of moving _code_ around. You  
either have to do it manually (there are several ways to do that) or  
you have to require the class needed for the operation to be present  
on the server already.

Ways how you can send over code:

- as a string of valid Ruby code (very insecure)
- as the name of a file to load on the server (a bit more secure)
- as a string of a particular DSL you invent for the task
- in your case you might be able to parametrize class generation and  
do it completely on the server

Kind regards

robert
