# Accessing Ruby Object in Memory

**URL:** <https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820>\
**Category:** Ruby\
**Created:** [October 13, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820 "2008-10-13T19:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 13, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820/1 "2008-10-13T19:42:29Z")

</div>

Hello all,

I have a long running Ruby simulation in which data is stored to Ruby  
hashes throughout the simulation. I’d like to be able to access the  
hashes in memory from another Ruby script while the simulation is still  
running to see what the progress of the simulation is (rather than  
printing to the screen, using log4r, etc. Is this possible? If so, any  
suggestions how to do so?

–  
Thanks!  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 13, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820/2 "2008-10-13T19:50:00Z")

</div>

Bryan R. wrote:

> Hello all,
> 
> I have a long running Ruby simulation in which data is stored to Ruby  
> hashes throughout the simulation. I’d like to be able to access the  
> hashes in memory from another Ruby script while the simulation is still  
> running to see what the progress of the simulation is (rather than  
> printing to the screen, using log4r, etc. Is this possible? If so, any  
> suggestions how to do so?

Hello. I guess this is not possible, and even if it is, it is not a good  
idea to do this. Probably a much better idea would be to start a TCP  
server in the daemon, and connect to it from the other program, and  
communicate using some simple protocol. Another idea could be to write  
some progress info into a file, and open the file from the other  
process, but then you’ll have to handle some concurrency problems (not  
very hard, I guess, it will be enough to wait a moment and retry on file  
opening failure). But still I think the server is a better and more  
standard way to do this. Have a look at TCPServer and TCPSocket in  
‘socket’, it is not hard.

TPR.

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 13, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820/3 "2008-10-13T21:22:45Z")

</div>

Thomas B. wrote:

> Hello. I guess this is not possible, and even if it is, it is not a good  
> idea to do this. Probably a much better idea would be to start a TCP  
> server in the daemon, and connect to it from the other program, and  
> communicate using some simple protocol. Another idea could be to write  
> some progress info into a file, and open the file from the other  
> process, but then you’ll have to handle some concurrency problems (not  
> very hard, I guess, it will be enough to wait a moment and retry on file  
> opening failure). But still I think the server is a better and more  
> standard way to do this. Have a look at TCPServer and TCPSocket in  
> ‘socket’, it is not hard.

DRb is the canonical way of doing this if both processes are ruby  
programs. For example:

== server.rb ==  
require ‘drb’

uri = “drbunix:/tmp/my-simulation.sock” # use “localhost:4444” on win  
h = {1=\>2, 3=\>4}  
DRb.start\_service(uri, h)  
DRb.thread.join

== client.rb ==  
require ‘drb’

uri = “drbunix:/tmp/my-simulation.sock” # use “localhost:4444” on win  
DRb.start\_service(nil, nil)  
server\_object = DRbObject.new(nil, uri)

p server\_object.keys  
server\_object.each do |k, v|  
p [k, v]  
end

================  
Output:

[1, 3]  
[1, 2]  
[3, 4]

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 13, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820/4 "2008-10-13T21:25:42Z")

</div>

On 13.10.2008 19:39, Bryan R. wrote:

> I have a long running Ruby simulation in which data is stored to Ruby  
> hashes throughout the simulation. I’d like to be able to access the  
> hashes in memory from another Ruby script while the simulation is still  
> running to see what the progress of the simulation is (rather than  
> printing to the screen, using log4r, etc. Is this possible? If so, any  
> suggestions how to do so?

Please look into DRb. For pure status updates I would push the current  
state to the other process as opposed to pulling it because this does  
not need synchronization in the simulation. You can find a working toy  
example attached.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [October 13, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/accessing-ruby-object-in-memory/150820/5 "2008-10-13T21:35:43Z")

</div>

Two addenda:

1. if the comm seems slow, try setting

Socket.do\_not\_reverse\_lookup = true

1. the url for windows (and other tcp) should be:

uri = “druby://localhost:4444”

So here’s the example again:

== server.rb ==  
require ‘drb’

Socket.do\_not\_reverse\_lookup = true

uri = “druby://localhost:4444”  
h = {1=\>2, 3=\>4}  
DRb.start\_service(uri, h)  
DRb.thread.join

== client.rb ==  
require ‘drb’

Socket.do\_not\_reverse\_lookup = true

uri = “druby://localhost:4444”  
DRb.start\_service(nil, nil)  
server\_object = DRbObject.new(nil, uri)

p server\_object.keys  
server\_object.each do |k, v|  
p [k, v]  
end
