Project

General

Profile

Actions

Backport #4273

closed

Inherited hook called after module_eval for Class.new

Added by banister (john mair) almost 14 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
[ruby-core:34426]

Description

=begin
class Base
def self.inherited(klass)
klass.instance_variable_set(:@x, :base)
end
end

Derived = Class.new(Base) do
@x = :derived
end

BUG HERE!

Derived.instance_variable_get(:@x) #=> :base

In Ruby 1.9 the line above returns :derived. Ruby 1.9 has the expected behaviour as the inherited hook should be invoked prior to running the block -- so that the block can override any ivar defaults defined by the inherited hook.

The problem lies in the following code, from object.c:

static VALUE
rb_class_initialize(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE super;

 if (RCLASS(klass)->super != 0) {
rb_raise(rb_eTypeError, "already initialized class");
 }
 if (rb_scan_args(argc, argv, "01", &super) == 0) {
super = rb_cObject;
 }
 else {
rb_check_inheritable(super);
 }
 RCLASS(klass)->super = super;
 rb_make_metaclass(klass, RBASIC(super)->klass);
  • rb_mod_initialize(klass);
  • rb_class_inherited(super, klass);
 return klass;

}

The fix would be simple, just swap the two starred lines above.

thanks,

John
=end

Actions #1

Updated by zimbatm (zimba tm) almost 14 years ago

=begin
Hi,

unfortunately, changing these two lines would break backward-compatibility, which would bring the same situation between the 1.8.7 release and the next-one. The only solutions I see for you is to either not rely on that feature, either discard 1.8 backward-compatibility.

I added this problem to this page that I just created : http://redmine.ruby-lang.org/wiki/ruby-19/MigrationIssuesFrom18

Cheers,
zimbatm
=end

Actions #2

Updated by jeremyevans0 (Jeremy Evans) over 5 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby 1.8 to Backport187
  • Description updated (diff)
  • Status changed from Open to Closed
  • Target version deleted (Ruby 1.8.7)
  • ruby -v deleted (ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32])
Actions

Also available in: Atom PDF

Like0
Like0Like0