strange behavior under mod_ruby

3 views
Skip to first unread message

Colin Steele

unread,
Mar 31, 2001, 9:44:07 PM3/31/01
to

The following script:

puts "MOD_RUBY<P>" if defined? MOD_RUBY
before = String.methods.sort
class String
end
after = String.methods.sort
if before != after
raise "String is now different..."
end

require "cgi"

before = CGI.methods.sort
class CGI
end
after = CGI.methods.sort
if before != after
puts "CGI is now different..."
diff = before - after
puts diff
end

Works fine (no output) if run from the command line. But, run in the
mod_ruby context, I get:

MOD_RUBY

CGI is now different... escape escapeElement escapeHTML parse
pretty rfc1123_date unescape unescapeElement unescapeHTML

I'm very confused. How is it that I'm er... "losing" all these
methods?

--
Colin Steele
co...@webg2.com / www.colinsteele.org / www.rubycookbook.org


From "The Hacker's Dictionary":
beam: [from Star Trek Classic's "Beam me up, Scotty!"] vt. To
transfer {softcopy} of a file electronically; most often in
combining forms such as `beam me a copy' or `beam that over to
his site'. Compare {blast}, {snarf}, {BLT}.


ts

unread,
Apr 1, 2001, 6:47:58 AM4/1/01
to
>>>>> "C" == Colin Steele <co...@webg2.com> writes:

Just add

C> before = CGI.methods.sort

puts CGI.name, "<P>"

C> class CGI
C> end

puts CGI.name, "<P>"

C> after = CGI.methods.sort


mod_ruby use rb_load_protect() with wrap = 1, this mean that your code is
run in a new anonymous module.

The class CGI that you define is a new class under this anonymous module.


Guy Decoux

Bryan Zarnett

unread,
Apr 1, 2001, 10:39:20 AM4/1/01
to
Morning,

I've been trying this morning to dynamically
instantiate an object through the use of a String that
represents the name of the class.

I am looking at doing something similar to Javas:

Class.forName("Foo").newInstance()

B.

_______________________________________________________
Do You Yahoo!?
Get your free @yahoo.ca address at http://mail.yahoo.ca

ts

unread,
Apr 1, 2001, 11:04:53 AM4/1/01
to
>>>>> "B" == Bryan Zarnett <bryan_...@yahoo.ca> writes:

B> I am looking at doing something similar to Javas:

B> Class.forName("Foo").newInstance()

When you define a class or a module, ruby define a constant. This mean
that in your case you can write

Module.const_get("Foo").new

When a class is defined under another class you can write something like
this

pigeon% cat b.rb
#!/usr/bin/ruby
class Toto
class Foo
def initialize
p "new Toto::Foo"
end
end
end

name = "Toto::Foo"
mod = Module
name.split(/::/).each {|m| mod = mod.const_get(m) }
mod.new
pigeon%

pigeon% b.rb
"new Toto::Foo"
pigeon%

You can also use #eval if you want


Guy Decoux

Dennis Decker Jensen

unread,
Apr 1, 2001, 11:33:32 AM4/1/01
to
Hi,

# I've been trying this morning to dynamically
# instantiate an object through the use of a String that
# represents the name of the class.
#
# I am looking at doing something similar to Javas:
#
# Class.forName("Foo").newInstance()

class A
def hello
"hello"
end
end
klassStr="A"

Klass=ObjectSpace.const_get(klassStr)
# or a little riskier:
# Klass=eval("#{klassStr}")

Klass.new.hello.display # -> hello


--
Dennis

Reply all
Reply to author
Forward
0 new messages