Sending variable from a controller to other file.rb

Hi, I was attempting to send a variable from a controller to another
ruby file that I have in the root of my application. The thing is that I
can’t send the value in any way.

I tryied using sessons, instance variables and I only get nils.

Someone know how to do this?

session[:telefono_rem] = nil
@envio = Envio.find params[:id]
if @envio.telefono_remitente.nil?
    session[:telefono_rem] = '00000000'
else
    session[:telefono_rem] = @envio.telefono_remitente
end
system("ruby " + PPAL + "WWW.rb")

===========================================================
WWW.rb

File.open(R, “a”) do |f|
f.puts “( WWW” + number.to_s
f.puts “|||||||||||”
f.puts "|||||: " + session[:telefono_rem].to_s
end#open

With this code it does’t print the last line.

Because you are invoking another ruby interpreter it won’t share
anything with your original process.

Can you simply pass the session[:telefono_rem] value to WWW.rb as a
commandline arg or read it from stdin? Or if you want the whole session
you could use Marshal to serialise it (depends what session contains).
This is probably the fastest and easiest way to achieve what you appear
to be after without changing very much at all.

Alternatively you could load the code from WWW.rb and run it directly,
or go to town and make WWW.rb a socket based service for your controller
to connect to and pass information via a user defined protocol.

If it is a local filesystem WWW.rb could use inotify (*nix) to watch a
file for new entries that your controller writes into. You could spin up
a redis or some such database and use that for IPC. You could use a
messaging service like AMQP, or in *nix land a named pipe. There are a
lot of ways to pass data between processes. It depends on what needs to
be achieved and why you are doing it/ how fast it needs to be.

I think there is quite a bit of code in the context that is not shown,
so it is hard to gauge whether I’ve correclty interpreted your intention
or not.

Sam

On Tue, Jul 19, 2011 at 07:19:54AM +0900, art tav wrote:

Hi, I was attempting to send a variable from a controller to another
ruby file that I have in the root of my application. The thing is that I
can’t send the value in any way.

If you’re using a halfway sane platform, you could pipe a text stream
between two programs easily enough.

If not, maybe your platform of choice supports sockets.

There is a number of choices some of which have been mentioned already.
What is appropriate depends on the use case which is not entirely clear.

  1. Use a command line option to transfer the value. This is most
    explicit and makes the interface of the sub process visible. Conversion
    can even be made automatic with OptionParser.

  2. Get rid of the split into two processes. Since the call is
    synchronous your calling process must wait anyway so it can as well do
    all the processing. This may introduce memory leaks though depending on
    the code in WWW.rb and the lifetime of the calling process.

  3. Set an environment variable (via ENV) before invoking the other Ruby
    process which then can read the value from there. This is far less
    obvious than the variant mentioned before and requires additional
    conversion if other values than strings must be transferred.

  4. Keep the local variable but instead of system use fork and load.
    This avoids conversions but some care should be taken with regard to
    open file descriptors.

Of course you can invent even more complex schemes (e.g. two processes
with DRb connection) but I have the impression that you are reinventing
the wheel. There are working solutions for web services already.

Kind regards

robert

Thanks to everybody, I solved this issue using the solution of the
command line arg because I think is the one that need less resources and
I don’t need to the information to be persistent