# Ruby bug?

**URL:** <https://rubytalk.org/t/ruby-bug/12372>\
**Category:** ruby-talk\
**Created:** [1 July 2004 22:17 UTC](https://rubytalk.org/t/ruby-bug/12372 "2004-07-01T22:17:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marek\_Janukowicz](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@Marek\_Janukowicz](https://rubytalk.org/u/Marek_Janukowicz)\
**Post date:** [1 July 2004 22:17 UTC](https://rubytalk.org/t/ruby-bug/12372/1 "2004-07-01T22:17:48Z")

</div>

It is not. You cannot "redefine" class in Ruby - if you open the class again (as  
in bug2.rb file) and define some methods in it, they will be \_added\_ to the  
existing class.

> **···**
>
> On Wed, 30 Jun 2004 17:00:04 -0500, DaZoner wrote:
> 
> > Imagine you want to redefine a class at runtime. One way to do it is to load  
> > a different class definition at runtime.
> > 
> > So make two class definitions in separate files and a test script:
> > 
> > bug1.rb --------------------------------------------------------------------  
> > ----------
> > 
> > class Bug  
> > &nbsp;&nbsp;def method1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > bug2.rb --------------------------------------------------------------------  
> > ----------
> > 
> > class Bug  
> > &nbsp;&nbsp;def method2  
> > &nbsp;&nbsp;end  
> > end
> > 
> > bugtest.rb -----------------------------------------------------------------  
> > ------------
> > 
> > load "bug1.rb"
> > 
> > b = Bug.new  
> > list = b.methods  
> > list.each { |methodName| print " ",methodName }  
> > print "\n"
> > 
> > load "bug2.rb"
> > 
> > b = Bug.new  
> > list = b.methods  
> > list.each { |methodName| print " ",methodName }  
> > print "\n"
> > 
> > ----------------------------------------------------------------------------  
> > --
> > 
> > Using Ruby 1.8.1 the second printed line shows Bug to respond to both  
> > method1 and method2 even though bug2.rb does not define method1. Is this a  
> > bug?
> 
> --  
> Marek Janukowicz

---

<div class="post-metadata">

**Author:** ![Jeff\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Jeff\_Mitchell](https://rubytalk.org/u/Jeff_Mitchell)\
**Post date:** [1 July 2004 22:36 UTC](https://rubytalk.org/t/ruby-bug/12372/2 "2004-07-01T22:36:28Z")

</div>

There is no formal way to redefine a class, but you can do this,

irb(main):001:0\> class A ; end  
=\> nil  
irb(main):002:0\> Object.send(:remove\_const, :A)  
=\> A  
irb(main):003:0\> A.new  
NameError: uninitialized constant A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3

> **···**
>
> --- Marek Janukowicz \<childNOSPAM@t17.ds.pwr.wroc.pl\> wrote:
> 
> > \>  
> > \> Using Ruby 1.8.1 the second printed line shows Bug to respond to both  
> > \> method1 and method2 even though bug2.rb does not define method1. Is this a  
> > \> bug?
> > 
> > It is not. You cannot "redefine" class in Ruby - if you open the class again (as  
> > in bug2.rb file) and define some methods in it, they will be \_added\_ to the  
> > existing class.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...
