Overriding methods in inherited classes

Hello all, I recently messed around with modifying Ruby’s CGI class in order
to make it return strings instead of arrays. The code to do so was simple:

require 'cgi’
class CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
end

This works well except that modruby caches classes, so all of my old Ruby
CGI programs break when they get strings instead of their expected arrays
since they’re using the cached copy of the new CGI class. My solution
to this problem was to create a derived class from CGI and us it in all
my new programs. I called it SCGI(scalar cgi), but I can’t get it to work.
Here’s my implementation:

require 'cgi’
class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
end

When I create an instance of it, it still returns all params as arrays instead
of strings. Am I missing something totally obvious here?

Thanks,
Travis Whitton whitton@atlantic.net

require ‘cgi’
class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
include QueryExtension
end

try that.

-a

···

On Fri, 31 Jan 2003, Travis Whitton wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Following my long withstanding tradition of following up my own posts, I
believe I have a solution. Does anybody see any problem with doing the
following?

require ‘cgi’

class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
def initialize
super
extend QueryExtension
end
end

Thanks,
Travis

require ‘cgi’
class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
include QueryExtension
end

try that.

It looked promising, but it didn’t work…

travis@travis travis $ cat scgitest.rb
require ‘scgi’
s = SCGI.new
p s[“foo”]

travis@travis travis $ ruby scgitest.rb
(offline mode: enter name=value pairs on standard input)
foo=bar
[“bar”]

-Travis

Travis,

···

On Sat, 1 Feb 2003 03:07:07 +0900 Travis Whitton whitton@atlantic.net wrote:

Following my long withstanding tradition of following up my own posts, I
believe I have a solution. Does anybody see any problem with doing the
following?

require ‘cgi’

class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end

Another possible solution:

def ; @params[key].to_s; end

I’m not sure about the behavior of cgi. method, though
we cannot predict the class of return object anyway in Ruby.

– susu

It looked promising, but it didn’t work…

eg/ruby > cat scgi.rb
require ‘cgi’
class SCGI < CGI
module QueryExtension
def
if @params[*args]
return @params[*args][0]
end
end
end
include QueryExtension
end
p SCGI.new[‘foo’]

~/eg/ruby > ruby -v
ruby 1.8.0 (2003-01-30) [i686-linux]

eg/ruby > ruby sgci.rb
(offline mode: enter name=value pairs on standard input)
foo=bar
“bar”

~/eg/ruby > ruby -v
ruby 1.6.7 (2002-03-01) [i686-linux]

eg/ruby > ruby scgi.rb
(offline mode: enter name=value pairs on standard input)
foo=bar
[“bar”]

looks like the behavior of cgi[*args] changed from 1.6.8 to 1.8.0. you will
have to work around this in 1.6.8.

-a

···

On Fri, 31 Jan 2003, Travis Whitton wrote:

travis@travis travis $ cat scgitest.rb
require ‘scgi’
s = SCGI.new
p s[“foo”]

travis@travis travis $ ruby scgitest.rb
(offline mode: enter name=value pairs on standard input)
foo=bar
[“bar”]

-Travis

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Yes, the behavior of cgi[‘key’] is changed !

ruby 1.6
value = cgi[‘key’] # [“value”]

ruby 1.8
value = cgi[‘key’] # “value”

Refer to [ruby-list:36846] (in Japanese)
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/36846

  • susu
···

On Sat, 1 Feb 2003 03:06:58 +0900 ahoward ahoward@fsl.noaa.gov wrote:

eg/ruby > ruby scgi.rb
(offline mode: enter name=value pairs on standard input)
foo=bar
[“bar”]

looks like the behavior of cgi[*args] changed from 1.6.8 to 1.8.0. you will
have to work around this in 1.6.8.