Ruby 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.

···

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
  def method1
  end
end

bug2.rb --------------------------------------------------------------------
----------

class Bug
  def method2
  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

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
        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