Class.new and class keyword not the same?

T

Trans

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

ara.t.howard

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.



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

Robert Dober

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.

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
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top