From: Bob Calco Date: 2002-09-03T22:41:34+09:00 Subject: RE: cgi redirect In Ruby, you can always add the method you want to CGI dynamically: require 'cgi' class CGI def redirect( where ) print this.header( { 'Status' => '302 Moved', 'location' => '#{where}' } ) end end Now whenever you create a new instance of CGI, you have that method, in addition to every other method in CGI. It's OK to complain in some langauges when a built in or library class doesn't have particular functionality you want/expect. But in Ruby it's much more productive to just add it when you need it. You can put those 7 grueling lines of code (including the space between "require" and "class CGI") into a separate file called mycgi.rb, plop it in your local site_ruby directory, and from now on all you need to write is: require 'mycgi' cgi = CGI.new cgi.redirect( "http://foo.bar.baz/" ) That's what I love about Ruby's OO model. Complain less, get more work done. Of course you could always subclass CGI, but then you lose CGI's private methods and data. Usually I'll only subclass when I need polymorphism or the problem domain cries out for a non-trivial object hierarchy. Otherwise, for most utilitatian purposes, just tweak an existing class using Ruby's various metaprogramming and dynamic OOP techniques. ;) Sincerely, Bob Calco %% -----Original Message----- %% From: Tom Robinson [mailto:tom@alkali.spamfree.org] %% Sent: Tuesday, September 03, 2002 8:42 AM %% To: ruby-talk ML %% Subject: cgi redirect %% %% %% in perl, this is easy: %% %% my $cgi = new CGI; %% print $cgi->redirect("http://foo.bar.baz/");' %% %% gives: %% Status: 302 Moved %% location: http://foo.bar.baz/ %% %% in ruby there is no CGI#redirect, the only way i could think of was: %% %% print cgi.header({'Status' => '302 Moved', 'location' => %% 'http://foo.bar.baz/'}) %% %% gives: %% Content-Type: text/html %% Status: 302 Moved %% location: http://foo.bar.baz/ %% %% nil %% irb(main):004:0> %% %% but it still prints the content-type header. any good way around this %% without doing it manually? i know there's a solution for mod_ruby, %% but cgi.rb really should have a redirect method as well i think. %% %% -- %% tom@alkali.spamfree.org %% remove 'spamfree.' to respond %% %%