Mod_ruby namespace question

Hello,

In mod_ruby, if I try something like:

class String
def foo
self+'foo’
end
end

'hello '.foo

then I will get an exception, because I was extending (well, creating, I
think) a new class called String, but it isn’t the same String as, say:

'hello '.class

I was wondering if this is intentional, or a side-effect of something else?

I can get around by doing something like:

fooDef = <<END_FOO
def foo
self+'foo’
end
END_FOO

'hello '.class.module_eval fooDef

or even just:

String.module_eval fooDef

Am I not supposed to do something like this? (I’m guessing that this would
add the foo method to every string in all of my scripts, which is fine for
me if that’s the only ‘gotcha’.) Or is that something in mod_ruby which
should be fixed?

Thanks,

Chris

Chris wrote:

In mod_ruby, if I try something like:

class String
def foo
self+‘foo’
end
end

'hello '.foo

then I will get an exception, because I was extending (well, creating, I
think) a new class called String, but it isn’t the same String as, say:

'hello '.class

I was wondering if this is intentional, or a side-effect of something else?

Intentional: From what I gather, mod_ruby wraps your code in an
anonymous module to protect you from nameclashes with other running
scripts. This makes it more difficult to make changes to standard
classes, with good reason, since you might “bork” one of the other
scripts that just happen to be runned using the same Apache process as
your script. (What if you both add String#foo with different behaviour?)
Net result, debug hell.

Am I not supposed to do something like this? (I’m guessing that this would
add the foo method to every string in all of my scripts, which is fine for
me if that’s the only ‘gotcha’.) Or is that something in mod_ruby which
should be fixed?

It is not the only gotcha, due to the shared, embedded interpreter. Such
wide-reaching changes really belong it its own .rb file that you include
specifically in the relevant section in your httpd.conf, but that might
seem overkill for small methods. I think you can factor it out into a
separate .rb file and require that in your script too. Requires are all
done on the toplevel, IIRC.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)