Cannot reverse string using drb :(

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]
#puts “still its f”
sleep(1)
end
object[0].each do |aa|
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
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!! :frowning:

On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan [email protected]
wrote:

start up the DRb service

object[0].each do |aa|

puts “all string reversed”


Posted via http://www.ruby-forum.com/.

If you check the DRB docs here
(Index of Classes & Methods in drb: Ruby Standard Library Documentation (Ruby 1.9.2)), 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.

“Jesús Gabriel y Galán” [email protected] wrote in post
#1078201:

On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan [email protected]
wrote:

start up the DRb service

object[0].each do |aa|

puts “all string reversed”


Posted via http://www.ruby-forum.com/.

If you check the DRB docs here
(Index of Classes & Methods in drb: Ruby Standard Library Documentation (Ruby 1.9.2)), you
will see:
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 like you and
understand it so nicely, 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? It’ll be great if you modify the
server object I am using here to make it easy for client to get the same
effect/result!

Jesus.

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

On Mon, Oct 1, 2012 at 7:24 PM, ajay paswan [email protected]
wrote:

Posted via http://www.ruby-forum.com/.

If you check the DRB docs here
(Index of Classes & Methods in drb: Ruby Standard Library Documentation (Ruby 1.9.2)), you
will see:
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:

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|
if Array === el
el.map{|s| s.reverse}
else
el
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.

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?

On Mon, Oct 1, 2012 at 8:56 PM, ajay paswan [email protected]
wrote:

Sorry being novice again:

What does map do?

and why map works here?

Because it is a method called on the remote object.

Jesus.

On Mon, Oct 1, 2012 at 7:34 PM, ajay paswan [email protected]
wrote:

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

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:
remote_array[0].length
remote_array[0][i]=whatever
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.