Can't inherit CGI from cgi.rb

  • Thread starter Dmitry Borodaenko
  • Start date
D

Dmitry Borodaenko

I thought it would be trivial to derive a class from CGI and add some
instance variables to it, however:

require 'cgi'
class SamizdatCGI < CGI
def initialize
super
@session = Session.new(self)
end
attr_reader :session
end
cgi = SamizdatCGI.new

As soon as I pass some parameters into such script, I get the following
error:

/usr/lib/ruby/1.6/cgi.rb:1595:in `class_eval': (eval):1:in
`remove_const': constant SamizdatCGI::CGI_PARAMS not defined (NameError)
from /usr/lib/ruby/1.6/cgi.rb:1595:in `class_eval'
from /usr/lib/ruby/1.6/cgi.rb:1595:in `initialize'
from /usr/lib/ruby/1.6/cgi.rb:1593

Offending code in cgi.rb is (it is the same in cgi.rb from Ruby 1.8):

initialize_query() # set @params, @cookies
eval "CGI_PARAMS = @params.nil? ? nil : @params.dup"
eval "CGI_COOKIES = @cookies.nil? ? nil : @cookies.dup"
if defined?(MOD_RUBY) and (RUBY_VERSION < "1.4.3")
raise "Please, use ruby1.4.3 or later."
else
at_exit() do
if defined?(CGI_PARAMS)
self.class.class_eval("remove_const:)CGI_PARAMS)")
^
self.class.class_eval("remove_const:)CGI_COOKIES)")
end
end
end

Is it a bug in cgi.rb, or in Ruby? If it's intended to not to allow to
inherit from CGI, is there some other elegant way to generate custom
instance variables from CGI.new ?
 
B

Brian Candler

I thought it would be trivial to derive a class from CGI and add some
instance variables to it, however:

require 'cgi'
class SamizdatCGI < CGI
def initialize
super
@session = Session.new(self)
end
attr_reader :session
end
cgi = SamizdatCGI.new

As soon as I pass some parameters into such script, I get the following
error:

/usr/lib/ruby/1.6/cgi.rb:1595:in `class_eval': (eval):1:in
`remove_const': constant SamizdatCGI::CGI_PARAMS not defined (NameError)
from /usr/lib/ruby/1.6/cgi.rb:1595:in `class_eval'
from /usr/lib/ruby/1.6/cgi.rb:1595:in `initialize'
from /usr/lib/ruby/1.6/cgi.rb:1593

Yeah, it's a bit wierd and inconvenient, and an unnecessary use of constants
IMO. It could certainly be made more flexible.

You can work around it; download ruby-fcgi-0.8.3 from RAA to see how it's
done.

Regards,

Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top