Rails — Ramdisk?

July 3rd, 2007 by Jacob Rus

Now that we have a Ruby client API, we need a demo! So I downloaded Ruby on Rails, opened up the famous screencast, and started making my chat demo. But Python (and CherryChat) knows how to keep its state in a regular variable, whereas it wasn’t clear to me (newbie that I am) how to do that in Ruby and Rails. So I decided, as any self-respecting programmer likely would, to ask on IRC. Specifically, in the #rubyonrails channel on freenode. The discussion that resulted was a classic (long enough that I’m putting it below the fold)

Chat from irc://irc.freenode.net/#rubyonrails,
2007-06-27 02:29:08 -0700

The names have been changed to protect the clueless
------------------------------------------------------------------

<jacobolus> hello

<jacobolus> I am trying to make a simple hello world page with a
            counter for how many times the page has been viewed.
            Where can i store my counter variable such that it
            isn't re-initialized each time someone visits the page?

<railsfan1> jacobolus: in a database

<jacobolus> i just want something transitory, in memory

<railsfan1> jacobolus: you want to keep the counter in ram ?

<jacobolus> yes

<railsfan1> jacobolus: weird, but possible. put it manually in
            a file and put this file in a ramdisk

<jacobolus> railsfan1:  are you kidding?

<railsfan1> jacobolus: no, why

<railsfan2> whats wrong with a file on disk its not like its
            going to be a perf bottleneck

<jacobolus> railsfan1:  well, i really just one to save one
            integer. it seems like a big deal to make a ramdisk
            for that

<railsfan1> jacobolus: well it is made difficult because of
            your very unusual requirement of keeping it in ram

<railsfan1> jacobolus: storing it in a database is trivial
            but you dont want to do that, you said

<jacobolus> railsfan1:  so there is never global state kept
            in a rails app?

<railsfan1> jacobolus: i would say no, but im not sure, you
            could kludge something in the environment.rb

<railsfan3> jacobolus, i dont think rails will do that

<jacobolus> hmm, how do i get access to stuff in the 
            environment.rb from my controller?

<railsfan3> im not 100% sure 

<railsfan3> but the problem is you may eventually be running 
            several rails processes

<railsfan1> jacobolus: i think the issue here is your own
            requirement

<railsfan3> so unless ruby has some mechanism for sharing 
            data across vms?

<railsfan1> jacobolus: can you explain why it is so important
            to keep the counter in ram ?

<jacobolus> i'm not interested in scaling this

<railsfan2> jacobolus: how about an Enviroment var in the OS?

<jacobolus> railsfan1:  because I don't want to use a database.
            i want to keep the dependancies as simple as possible

<railsfan1> the os env var will not work for sure : )

<railsfan3> jacobolus, ah ok, if it's just the one process
            then you ought to be able to do it with a global 
            variable or something like that

<railsfan2> jacobolus: or a simple flat file i you really must
            have it reset when server reboots you could do an
            uptime check and clear or somthing

<railsfan1> jacobolus: ok then store it in a file in /tmp

<railsfan3> just remember that you've done it before you decide
            to run two instances : )

<jacobolus> how would i access a global variable?

<jacobolus> railsfan3:  i have no intention of ever running a
            two instance rail app

<railsfan1> jacobolus: you should read about cgi/fast-cgi and
            you will understand why it is hard to do

<railsfan2> railsfan1: why would a env var not work, rails runs
            under one user

<jacobolus> railsfan1:  i know how both of those work and i
            still don't understand

<railsfan2> not arguing just trying to understand

<jacobolus> railsfan1:  in cgi it makes sense

<railsfan1> railsfan2: env var are not shared, they are copied. 
            aka modifying your own will not affect the other copy

<jacobolus> railsfan1:  but for fastcgi it should be trivial

<railsfan2> ohh thought that is what export did my bad

<railsfan1> jacobolus: ok try to define your stuff in 
            environment.rb, it *may* do the trick

<jacobolus> railsfan1: how can i access the stuff in environment.rb 
            from inside my controller?

<railsfan1> jacobolus: environment.rb is read by 'everybody'. but
            it is done for read only var in theory. maybe you could
            kludge it to support write too

<railsfan1> jacobolus: those are global ruby variable. google to
            get example

<jacobolus> I don't understand how the rails server interacts with 
            the controller code... does it re-interpret it for each
            request?

<railsfan2> jacobolus: yes

<railsfan2> jacobolus: the web is mainly statless thats why we have
            to dick about with sessions

One Response to “Rails — Ramdisk?”

  1. Orbited Blog » Blog Archive » RailsChat Tutorial Says:

    [...] a site. But I suppose it works well enough for what it’s designed for. After giving up on the discussion in the #rubyonrails channel, I asked some Ruby programmer friends how to save state, and they explained to me that adding $ [...]

Leave a Reply