Forum: Ruby override def method in CGI class, without altering the class

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
674a19cc2cd50025003f4d198bbfa6fb?d=identicon&s=25 Jer A (Guest)
on 2007-05-24 02:25
(Received via mailing list)
hello all,

I am a newbie to ruby.

how do I override a method from a particular class (in std class lib),
for one program, without modifying the class.

Thanks in advance for all your help.

-Jer A.
Fb971283c36f4e1738f21ec42bc087a7?d=identicon&s=25 highlyjhi (Guest)
on 2007-05-24 04:25
(Received via mailing list)
Just re-write it in your program where it's needed.

class Foo
   def bar
     //method to override

Very simple.

- Jer H.
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-05-24 08:07
(Received via mailing list)
highlyjhi wrote:
> - Jer H.
>> just for one program, without modifying the class.

Just to clarify, you can define or redefine methods of core classes:

Class String
   def inspect
     "This string's value is \"#{self}\""
This topic is locked and can not be replied to.