But is there a way to call the original method instead of just quitting
out?


On Thu, 2010-09-30 at 14:30 -0500, Jeremy Bopp wrote:
> On 9/30/2010 2:15 PM, Stefan Salewski wrote:
> > In Ruby we can add new methods to existing classes.
> > How can we ensure that the name of our new method does not already
> > exists -- I do not want to overwrite an existing method by accident. I
> > think there exists a way to list all currently existing methods of a
> > class, but the solution should work in future too( e.g. for Ruby 3.0
> > when predefined classes may have more predefined methods): I want
> > something like
> > 
> > class String
> >   def very_useful_method
> >     if already_defined(this_method)
> >       puts 'We are overwriting an existing method by accident'
> >       Process.exit
> >     end
> >   end
> > end
> 
> This should do the trick:
> 
> class String
>   unless method_defined?(:my_method) then
>     def my_method
>     end
>   else
>     puts 'We tried to overwrite an existing method'
>     Process.exit
>   end
> end
> 
> 
> -Jeremy
>