Pass variables between separate instances of ruby (without writing to a text file or database)

Posted by boulder_ruby on Stack Overflow See other posts from Stack Overflow or by boulder_ruby
Published on 2013-10-12T20:21:21Z Indexed on 2013/10/18 3:55 UTC
Read the original article Hit count: 128

Lets say I'm running a long worker-script in one of several open interactive rails consoles.

The script is updating columns in a very, very, very large table of records. I've muted the ActiveRecord logger to speed up the process, and instruct the script to output some record of progress so I know how roughly how long the process is going to take. That is what I am currently doing and it would look something like this:

ModelName.all.each_with_index do |r, i|
  puts i if i % 250
  ...runs some process...
  r.save
end

Sometimes its two nested arrays running, such that there would be multiple iterators and other things running all at once.

Is there a way that I could do something like this and access that variable from a separate rails console? (such that the variable would be overwritten every time the process is run without much slowdown)

records = ModelName.all
$total = records.count
records.each_with_index do |r, i|
  $i = i
  ...runs some process...
  r.save
end

meanwhile mid-process in other console

puts "#{($i/$total * 100).round(2)}% complete"
#=> 67.43% complete

I know passing global variables from one separate instance of ruby to the next doesn't work. I also just tried this to no effect as well

unix console 1

 $X=5
 echo {$X}
 #=> 5

unix console 2

echo {$X}
#=> ""

Lastly, I also know using global variables like this is a major software design pattern no-no. I think that's reasonable, but I'd still like to know how to break that rule if I'd like.

Writing to a text file obviously would work. So would writing to a separate database table or something. That's not a bad idea. But the really cool trick would be sharing a variable between two instances without writing to a text file or database column.

What would this be called anyway? Tunneling? I don't quite know how to tag this question. Maybe bad-idea is one of them. But honestly design-patterns isn't what this question is about.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about global-variables