Class.new and class keyword not the same?


#1

Here the normal scenario:

class X
def self.inherited(s)
p “here”
end
end

class Z < X
p “race”
end

result:

“here”
“race”

But in a test case of mine I need the class to be a variable, but:

z = Class.new(X) do
p “race”
end

result:

“race”
“here”

Why is “race” coming before “here”?

ruby 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux]

T.


#2

On Nov 14, 2008, at 9:09 AM, Trans wrote:

end
end
T.

dunno why - but the fix is

cfp:~ > cat a.rb
class X
def self.inherited(s)
p “here”
end
end

z = Class.new(X)
z.class_eval do
p “race”
end

class Z < X
p “race”
end

cfp:~ > ruby a.rb
“here”
“race”
“here”
“race”

a @ http://codeforpeople.com/


#3

On Fri, Nov 14, 2008 at 5:09 PM, Trans removed_email_address@domain.invalid wrote:

end
end
T.

Strange, it is “fixed” in 1.9 (though I have an old version ruby 1.9.0
(2008-06-20 revision 17482) [i486-linux]
). But but then Jruby behaves like 1.8, maybe this is defined
somewhere?
If we look at the warning below it really seems like a bug, 'cause the
old method is not discarded.

516/16 > cat xxx.rb && ruby1.9 -v xxx.rb && ruby -v xxx.rb && jruby -v
xxx.rb
class X
def self.inherited(x);
x.send :define_method, :a do puts 42 end
end
end
class Y < X
def a; puts “21*2” end
end
Y::new.a
puts "-"72
Class::new Y do
def a; puts “222.to_i(4)” end
end::new.a
ruby 1.9.0 (2008-06-20 revision 17482) [i486-linux]
xxx.rb:11: warning: method redefined; discarding old a
21
2

xxx.rb:16: warning: method redefined; discarding old a
222.to_i(4)
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]
xxx.rb:11: warning: method redefined; discarding old a
21*2

42
ruby 1.8.6 (2008-05-30 rev 6360) [i386-jruby1.1]
21*2

42

Any views on this?

Cheers
Robert