Ruby bug?

D

DaZoner

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"

----------------------------------------------------------------------------
 
T

Tim Hunter

DaZoner said:
Imagine you want to redefine a class at runtime. One way to do it is to
load a different class definition at runtime.
....

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?

This is Ruby working as designed. The 2nd file doesn't replace the Bug
class, it "opens" it for changes and adds a new method. All classes may be
opened and modified dynamically.

If you want to remove method1, look at undef_method or remove_method.
 
M

Marek Janukowicz

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?

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

Jeff Mitchell

--- Marek Janukowicz said:
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.

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


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top