# Class.new and class keyword not the same?

**URL:** <https://www.ruby-forum.com/t/class-new-and-class-keyword-not-the-same/153306>\
**Category:** Ruby\
**Created:** [November 14, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/class-new-and-class-keyword-not-the-same/153306 "2008-11-14T17:13:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 14, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/class-new-and-class-keyword-not-the-same/153306/1 "2008-11-14T17:13:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 14, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/class-new-and-class-keyword-not-the-same/153306/2 "2008-11-14T17:23:46Z")

</div>

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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 15, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/class-new-and-class-keyword-not-the-same/153306/3 "2008-11-15T16:09:36Z")

</div>

On Fri, Nov 14, 2008 at 5:09 PM, Trans [removed\_email\_address@domain.invalid](mailto: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
