Mod_ruby: What is the lifetime of my objects?

Level:
Still new to ruby and CGIscripting I am working on my first web scripts.

Setting:

form1.rbx --> form2.rbx

with both scripts needing the objects instantiated in ‘classes.rb’.

Question:

Assuming I put ‘require “classes.rb”’ at the beginning of form1.rbx
When I click Submit in form1.rbx and get to form2.rbx, what happens to
the objects instantiated in ‘classes.rb’ in the following 3 cases:

  • form2.rbx contains a ‘require classes.rb’ statement
  • form2.rbx contains a ‘load classes.rb’ statement
  • form2.rbx contains neither

Do they disappear ? Can I still access them ?

Thank you for your time,
Simon

···


If one cannot enjoy reading a book over and over again, there is no use
in reading it at all.
– Oscar Wilde

Unless you’re instantiating the objects in an object store or
another server-side object environment, the lifetime of a given
object is the lifetime of the webpage/CGI script.

Thus, when you ‘require “classes.rb”’, unless classes.rb stores the
current state of your objects (and not just the default state), then
you’re just getting the default objects. Note that having classes.rb
store the current state is a very BAD idea – it’s not scalable past
one user.

The difference between ‘require’ and ‘load’ is that ‘load’ will
ALWAYS load the file requested, whereas ‘require’ will load it only
if it hasn’t already been loaded (but there’s apparently a way to
fool require).

If you want to pass your classes between web pages, you either need
to instantiate a session in an object store/object environment (like
is done with EJB) and reference your objects by session, or pass
them along as hidden parameters (noting that if you do, they may be
subject to improper modification and you probably want to have some
sort of MD5 hash on some or all of the values that also goes out as
part of the hidden parameters).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.12.25 at 13.57.29

···

On Thu, 26 Dec 2002 02:46:32 +0900, Vandemoortele Simon wrote:

Assuming I put ‘require “classes.rb”’ at the beginning of
form1.rbx When I click Submit in form1.rbx and get to form2.rbx,
what happens to the objects instantiated in ‘classes.rb’ in the
following 3 cases:

  • form2.rbx contains a ‘require classes.rb’ statement
  • form2.rbx contains a ‘load classes.rb’ statement
  • form2.rbx contains neither
    Do they disappear ? Can I still access them ?

Hmmm, this is indeed the behaviour I would expect. Still I wonder why I
get the apache warning message
…/classes.rb:15: warning: already initialized constant PictureForm
The warning seems to suggest the constant instantiated when form1.rbx
loaded ‘classes.rb’ still existed when form2.rbx loaded ‘classes.rb’ …

Is that possible ? If so, doesn’t that contradict the above ?

Simon

···

Austin Ziegler austin@halostatue.ca wrote:

Unless you’re instantiating the objects in an object store or
another server-side object environment, the lifetime of a given
object is the lifetime of the webpage/CGI script.

Thus, when you ‘require “classes.rb”’, unless classes.rb stores the
current state of your objects (and not just the default state), then
you’re just getting the default objects. Note that having classes.rb
store the current state is a very BAD idea – it’s not scalable past
one user.

The difference between ‘require’ and ‘load’ is that ‘load’ will
ALWAYS load the file requested, whereas ‘require’ will load it only
if it hasn’t already been loaded (but there’s apparently a way to
fool require).

If you want to pass your classes between web pages, you either need
to instantiate a session in an object store/object environment (like
is done with EJB) and reference your objects by session, or pass
them along as hidden parameters (noting that if you do, they may be
subject to improper modification and you probably want to have some
sort of MD5 hash on some or all of the values that also goes out as
part of the hidden parameters).


If one cannot enjoy reading a book over and over again, there is no use
in reading it at all.
– Oscar Wilde