From: "Jesús Gabriel y Galán" Date: 2012-10-02T01:56:04+09:00 Subject: Re: cannot reverse string using drb :( On Mon, Oct 1, 2012 at 6:34 PM, ajay paswan 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] > #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!! :( > > -- > Posted via http://www.ruby-forum.com/. > If you check the DRB docs here (http://ruby-doc.org/stdlib-1.9.2/libdoc/drb/rdoc/index.html), 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.