Strange mod_ruby

This seems to be a problem of mod_ruby.
Ruby itself and irb handle this right.

I execute the following code with mod_ruby (Linux, Apache, all code to
handle the http is cut away):

— SNIP —
class Fixnum
def foo
’foo:’<<to_s
end
end

i=2
puts i.class.to_s
puts i.foo
— SNIP —

and I expect the following output:

#> Fixnum
#> foo:2

But I get the error: “undefined method `foo’ for 2:Fixnum (NoMethodError)”

This seems very strange to me?!?

bye!
Dominik

This seems to be a problem of mod_ruby.
Ruby itself and irb handle this right.

···

----- Original Message -----
From: “Dominik Werder” dwerder@gmx.net


It’s because you are wrapped in an anonymous module when you use mod_ruby,
so you just created a new Fixnum class in that module, which has nothing at
all to do with 2.

If you put that code into another file and require' it, it gets added to the root namespace. I think you can also do something like2.class.module_eval { … }’, IIRC.

Chris

It’s because you are wrapped in an anonymous module when you use
mod_ruby,
so you just created a new Fixnum class in that module, which has nothing
at
all to do with 2.

If you put that code into another file and require' it, it gets added to the root namespace. I think you can also do something like 2.class.module_eval { … }', IIRC.

thank you very much, this one got me really busy the last 2 days :slight_smile:

is there a reason for this kind of design?

bye!
Dominik

The main reason is that mod_ruby has a single instance of the Ruby
interpreter for all the scripts which are being run under it. If one
script decides to define something in its top-level namespace, you don’t
want that thing to appear in the top-level namespace of all other scripts -
it would couple them together too closely, causing hard-to-find bugs.

So each script in run in its own anonymous namespace. But if they ‘require’
a library, then that goes into the shared top-level namespace: it doesn’t
make sense for each script to have its own copy of the dbi library, for
example.

Cheers,

Brian.

···

On Wed, May 21, 2003 at 04:56:51PM +0900, Dominik Werder wrote:

It’s because you are wrapped in an anonymous module when you use
mod_ruby,
so you just created a new Fixnum class in that module, which has nothing
at
all to do with 2.

If you put that code into another file and require' it, it gets added to the root namespace. I think you can also do something like 2.class.module_eval { … }', IIRC.

thank you very much, this one got me really busy the last 2 days :slight_smile:

is there a reason for this kind of design?

thank you very much, this one got me really busy the last 2 days :slight_smile:
is there a reason for this kind of design?

The main reason is that mod_ruby has a single instance of the Ruby
interpreter for all the scripts which are being run under it. If one

Does that mean that all apache processes use a single ruby interpreter or
do they have an interpreter on theire own?

script decides to define something in its top-level namespace, you don’t
want that thing to appear in the top-level namespace of all other scripts

it would couple them together too closely, causing hard-to-find bugs.

what should I do if I want to change the behavior of Fixnum in one project
but doesn’t want to disturb the other project?

thanks!
Dominik

So, how does one create an anonymous module (ie, one without a name
or a name that can be guaranteed to be unique)?

···

On Wednesday, 21 May 2003 at 19:44:34 +0900, Brian Candler wrote:

On Wed, May 21, 2003 at 04:56:51PM +0900, Dominik Werder wrote:
So each script in run in its own anonymous namespace. But if they ‘require’
a library, then that goes into the shared top-level namespace: it doesn’t
make sense for each script to have its own copy of the dbi library, for
example.


Jim Freeze

No animal should ever jump on the dining room furniture unless
absolutely certain he can hold his own in conversation.
– Fran Lebowitz

The main reason is that mod_ruby has a single instance of the Ruby
interpreter for all the scripts which are being run under it. If one

Does that mean that all apache processes use a single ruby interpreter or
do they have an interpreter on theire own?

As far as I understand, each worker process is independent and has its own
Ruby instance.

what should I do if I want to change the behavior of Fixnum in one project
but doesn’t want to disturb the other project?

I don’t know if that’s possible with mod_ruby. If you need that you could
use fastcgi instead (apache mod_fastcgi + ruby fcgi) where each
application has its own process or pool of processes.

Regards,

Brian.

···

On Wed, May 21, 2003 at 08:20:27PM +0900, Dominik Werder wrote:

Why don’t you make a singleton for the object instead of the class?

···

On Wednesday, 21 May 2003 at 20:20:27 +0900, Dominik Werder wrote:

what should I do if I want to change the behavior of Fixnum in one project
but doesn’t want to disturb the other project?


Jim Freeze

Love your enemies: they’ll go crazy trying to figure out what you’re up
to.

foo = Module.new

Or, to create an anonymous module and run some code inside it, use
Kernel#load(filename,true)

Cheers,

Brian.

···

On Thu, May 22, 2003 at 12:51:04PM +0900, Jim Freeze wrote:

On Wednesday, 21 May 2003 at 19:44:34 +0900, Brian Candler wrote:

On Wed, May 21, 2003 at 04:56:51PM +0900, Dominik Werder wrote:
So each script in run in its own anonymous namespace. But if they ‘require’
a library, then that goes into the shared top-level namespace: it doesn’t
make sense for each script to have its own copy of the dbi library, for
example.

So, how does one create an anonymous module (ie, one without a name
or a name that can be guaranteed to be unique)?

what should I do if I want to change the behavior of Fixnum in one
project but doesn’t want to disturb the other project?

I don’t know if that’s possible with mod_ruby. If you need that you could
use fastcgi instead (apache mod_fastcgi + ruby fcgi) where each
application has its own process or pool of processes.

will look at it :slight_smile:

thanks!
Dominik

what should I do if I want to change the behavior of Fixnum in one
project but doesn’t want to disturb the other project?
Why don’t you make a singleton for the object instead of the class?
because I want all new numbers to have this new function…

bye!
Dominik