# Cannot reverse string using drb :(

**URL:** <https://rubytalk.org/t/cannot-reverse-string-using-drb/66390>\
**Category:** ruby-talk\
**Created:** [1 October 2012 16:34 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390 "2012-10-01T16:34:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [1 October 2012 16:34 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/1 "2012-10-01T16:34:03Z")

</div>

Just playing with DRB

serv.rb:  
#!/usr/bin/env ruby -w  
# simple\_service.rb  
# A simple DRb service

# load DRb  
require 'drb'

# start up the DRb service  
a=["first","second","third","fourth"]  
b=false  
object=[a,b]  
DRb.start\_service 'druby://127.0.0.1:8008', object  
# We need the uri of the service to connect a client  
puts DRb.uri  
while !object[1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#puts "still its f"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
end  
object[0].each do |aa|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts aa  
end

> **···**
>
> ----------------------------------------------------------------  
> client.rb  
> #!/usr/bin/env ruby -w  
> # simple\_client.rb  
> # A simple DRb client
> 
> require 'drb'
> 
> DRb.start\_service
> 
> # attach to the DRb server via a URI given on the command line  
> remote\_array = DRbObject.new nil, ARGV.shift
> 
> strs= remote\_array[0]  
> flag = remote\_array[1]  
> for i in 0..remote\_array[0].length-1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_array[0][i]=remote\_array[0][i].reverse  
> end  
> puts "all string reversed"  
> remote\_array[1]=true  
> puts "flag made true to finish"
> 
> it prints:  
> first  
> second  
> third  
> fourth
> 
> But U know what I wanted!! ☹
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [1 October 2012 16:56 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/2 "2012-10-01T16:56:04Z")

</div>

If you check the DRB docs here  
([http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html\](http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html%5C)), you  
will see:

"Any type of object can be passed as an argument to a dRuby call or  
returned as its return value. By default, such objects are dumped or  
marshalled at the local end, then loaded or unmarshalled at the remote  
end. The remote end therefore receives a copy of the local object, not  
a distributed reference to it; methods invoked upon this copy are  
executed entirely in the remote process, not passed on to the local  
original. This has semantics similar to pass-by-value."

What this means is that the methods you call on "remote\_array" are  
passed to the server, executed in the server, and the result of the  
method call is marshalled and sent to the client. The client then  
unmarshalls it and has a local copy.  
So, you have two objects in your example: remote\_array and  
remote\_array[0], which is another array. When you do remote\_array[0],  
you are calling method on the remote array, this call is executed  
in the server. This call returns an array, which is is marshalled and  
unmarshalled, resulting in the client having another copy. Anything  
that you do over this array is only done locally.

To achieve what you want to do, you have to just call methods on the  
remote array. So something like this could work:

1.9.2p290 :041 \> remote\_array.map! do |el|  
1.9.2p290 :042 \> if Array === el  
1.9.2p290 :043?\> el.map{|s| s.reverse}  
1.9.2p290 :044?\> else  
1.9.2p290 :045 \> el  
1.9.2p290 :046?\> end  
1.9.2p290 :047?\> end  
=\> [["tsrif", "dnoces", "driht", "htruof"], false]  
1.9.2p290 :048 \> remote\_array[1] = true  
=\> true

Which as you see is a bit weird, but this is because the structure you  
chose. I know this is just an example, but it shows that you should  
model your server objects in such a way to make easy for the clients  
to do the stuff.

Jesus.

> **···**
>
> On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan \<lists@ruby-forum.com\> wrote:
> 
> > Just playing with DRB
> > 
> > serv.rb:  
> > #!/usr/bin/env ruby -w  
> > # simple\_service.rb  
> > # A simple DRb service
> > 
> > # load DRb  
> > require 'drb'
> > 
> > # start up the DRb service  
> > a=["first","second","third","fourth"]  
> > b=false  
> > object=[a,b]  
> > DRb.start\_service 'druby://127.0.0.1:8008', object  
> > # We need the uri of the service to connect a client  
> > puts DRb.uri  
> > while !object[1]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#puts "still its f"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
> > end  
> > object[0].each do |aa|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts aa  
> > end
> > 
> > ----------------------------------------------------------------  
> > client.rb  
> > #!/usr/bin/env ruby -w  
> > # simple\_client.rb  
> > # A simple DRb client
> > 
> > require 'drb'
> > 
> > DRb.start\_service
> > 
> > # attach to the DRb server via a URI given on the command line  
> > remote\_array = DRbObject.new nil, ARGV.shift
> > 
> > strs= remote\_array[0]  
> > flag = remote\_array[1]  
> > for i in 0..remote\_array[0].length-1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_array[0][i]=remote\_array[0][i].reverse  
> > end  
> > puts "all string reversed"  
> > remote\_array[1]=true  
> > puts "flag made true to finish"
> > 
> > it prints:  
> > first  
> > second  
> > third  
> > fourth
> > 
> > But U know what I wanted!! ☹
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [1 October 2012 17:24 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/3 "2012-10-01T17:24:46Z")

</div>

"Jesús Gabriel y Galán" \<jgabrielygalan@gmail.com\> wrote in post  
#1078201:

> > # start up the DRb service  
> > object[0].each do |aa|
> > 
> > puts "all string reversed"
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> If you check the DRB docs here  
> ([http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html\](http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html%5C)), you  
> will see:

&nbsp;&nbsp;where is it written, sorry to be stupid, but may be am asking these  
questions as I am not comfortable or cannot read doc's any help  
please..?

> 1.9.2p290 :041 \> remote\_array.map! do |el|  
> 1.9.2p290 :042 \> if Array === el  
> 1.9.2p290 :043?\> el.map{|s| s.reverse}  
> 1.9.2p290 :044?\> else

Its great to have so explained answer, but really I am unable to get the  
meaniing of codes, and what if I deal with a class's object?

> 1.9.2p290 :045 \> el  
> 1.9.2p290 :046?\> end  
> 1.9.2p290 :047?\> end  
> =\> [["tsrif", "dnoces", "driht", "htruof"], false]  
> 1.9.2p290 :048 \> remote\_array[1] = true  
> =\> true
> 
> Which as you see is a bit weird, but this is because the structure you  
> chose. I know this is just an example, but it shows that you should  
> model your server objects in such a way to make easy for the clients  
> to do the stuff.

Sorry, but can I get an example Sir?

> **···**
>
> > On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan \<lists@ruby-forum.com\> \> wrote:
> > 
> > Jesus.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [1 October 2012 17:34 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/4 "2012-10-01T17:34:19Z")

</div>

What is exactly map? is it enumerator? then what was the problem with my  
code using for? Kindly explain!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [1 October 2012 17:33 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/5 "2012-10-01T17:33:31Z")

</div>

> "Jesús Gabriel y Galán" \<jgabrielygalan@gmail.com\> wrote in post  
> #1078201:
> 
> > > # start up the DRb service  
> > > object[0].each do |aa|
> > > 
> > > puts "all string reversed"
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > If you check the DRB docs here  
> > ([http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html\](http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html%5C)), you  
> > will see:
> 
> &nbsp;&nbsp;where is it written, sorry to be stupid, but may be am asking these  
> questions as I am not comfortable or cannot read doc's any help  
> please..?

Sorry, I pasted the first link, you have to click on the DRb module:

> **[Module: DRb (Ruby 1.9.2)](https://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/DRb.html)**
>
> Module : DRb - Ruby 1.9.2

> > 1.9.2p290 :041 \> remote\_array.map! do |el|  
> > 1.9.2p290 :042 \> if Array === el  
> > 1.9.2p290 :043?\> el.map{|s| s.reverse}  
> > 1.9.2p290 :044?\> else
> 
> Its great to have so explained answer, but really I am unable to get the  
> meaniing of codes, and what if I deal with a class's object?
> 
> > 1.9.2p290 :045 \> el  
> > 1.9.2p290 :046?\> end  
> > 1.9.2p290 :047?\> end  
> > =\> [["tsrif", "dnoces", "driht", "htruof"], false]  
> > 1.9.2p290 :048 \> remote\_array[1] = true  
> > =\> true
> > 
> > Which as you see is a bit weird, but this is because the structure you  
> > chose. I know this is just an example, but it shows that you should  
> > model your server objects in such a way to make easy for the clients  
> > to do the stuff.
> 
> Sorry, but can I get an example Sir?

What I posted above is an example of the client code that goes with your server.

client.rb  
#!/usr/bin/env ruby -w  
# simple\_client.rb  
# A simple DRb client

require 'drb'

DRb.start\_service

# attach to the DRb server via a URI given on the command line  
remote\_array = DRbObject.new nil, ARGV.shift

remote\_array.map! do |el|  
&nbsp;&nbsp;if Array === el  
&nbsp;&nbsp;&nbsp;&nbsp;el.map{|s| s.reverse}  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;el  
&nbsp;&nbsp;end  
end

puts "all string reversed"  
remote\_array[1]=true  
puts "flag made true to finish"

> Its great to have so explained answer, but really I am unable to get the  
> meaniing of codes, and what if I deal with a class's object?

Well, in order to understand DRb you have to know that when you call a  
method on a remote object it's executed on the server. The result is  
passed to the client. If you call methods on this result, they are not  
passed anymore to the server:

remote\_array = DRbObject.new nil, ARGV.shift

first\_element = remote\_array[0] # the method with argument 0 is  
executed on the server. The result is an Array, which is marshalled  
and sent to the client. first\_element is a \*copy\* of the server's  
array.

first\_element[0] = "test" # now, this method = with arguments 0 and  
"test" is executed only on the client, cause it's done against a  
regular array, and not a DRbObject.

Jesus.

> **···**
>
> On Mon, Oct 1, 2012 at 7:24 PM, ajay paswan \<lists@ruby-forum.com\> wrote:
> 
> > > On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan \<lists@ruby-forum.com\> \>\> wrote:

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [1 October 2012 17:40 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/6 "2012-10-01T17:40:13Z")

</div>

I already explained it. I think you need to read the answers a bit  
slower. I'll say it again here:

remote\_array is a DRbObject. Methods called on it are executed on the server.  
remote\_array[0] is regular, local, array. Methods called on it are  
executed on the client.  
When you do things like:  
&nbsp;&nbsp;remote\_array[0].length  
&nbsp;&nbsp;remote\_array[0][i]=whatever  
&nbsp;&nbsp;remote\_array[0][i].reverse

all those things are executed only in the client. The reason is that  
the first thing those snippets do is remote\_array[0] which returns a  
regular, local array. Everything else after that is just in the  
client. That's why you never see the changes at the server side.

Jesus.

> **···**
>
> On Mon, Oct 1, 2012 at 7:34 PM, ajay paswan \<lists@ruby-forum.com\> wrote:
> 
> > What is exactly map? is it enumerator? then what was the problem with my  
> > code using for? Kindly explain!

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [1 October 2012 18:56 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/7 "2012-10-01T18:56:51Z")

</div>

Sorry being novice again:

What does map do?

and why map works here?

and how you could have defined this object to get the job done and  
client had to work not so wierd?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [2 October 2012 07:06 UTC](https://rubytalk.org/t/cannot-reverse-string-using-drb/66390/8 "2012-10-02T07:06:32Z")

</div>

> Sorry being novice again:
> 
> What does map do?

> **[Module: Enumerable (Ruby 1.9.3)](https://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-map)**
>
> Module : Enumerable - Ruby 1.9.3

> and why map works here?

Because it is a method called on the remote object.

Jesus.

> **···**
>
> On Mon, Oct 1, 2012 at 8:56 PM, ajay paswan \<lists@ruby-forum.com\> wrote:
