# C extensions and class constants nested within modules

**URL:** <https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704>\
**Category:** Ruby\
**Created:** [November 22, 2005, 6:55pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704 "2005-11-22T18:55:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel.Berger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Daniel.Berger](https://www.ruby-forum.com/u/Daniel.Berger)\
**Post date:** [November 22, 2005, 6:55pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704/1 "2005-11-22T18:55:05Z")

</div>

Hi all,

This appears to be perfectly legal Ruby code:

module Foo  
class Bar  
end  
class Bar::Baz  
end  
end

p Foo::Bar.new # ok  
p Foo::Bar::Baz.new # ok

However, an equivalent C extension has problems:

void Init\_foo(){  
VALUE mFoo = rb\_define\_module(“Foo”);  
VALUE cBar = rb\_define\_class\_under(mFoo, “Bar”, rb\_cObject);  
VALUE cBarBaz = rb\_define\_class\_under(mFoo, “Bar::Baz”, rb\_cObject);  
}

p Foo::Bar.new # ok  
p Foo::Bar::Baz.new # uninitialized constant Foo::Bar::Baz (NameError)

Why doesn’t that work?

The only way I could get it to work was to declare “Baz” under “Bar”.  
But is  
that really the same thing?

Regards,

Dan

PS - I may have asked this before, but I’m old, my mind is going and  
Google is  
failing me.

---

<div class="post-metadata">

**Author:** ![Daniel.Berger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Daniel.Berger](https://www.ruby-forum.com/u/Daniel.Berger)\
**Post date:** [November 22, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704/2 "2005-11-22T19:07:09Z")

</div>

> > > > > “D” == Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> Why doesn’t that work?

Because this is parse.y and eval.c which make work

class Bar::Baz  
end

and not the call to rb\_define\_class()

With rb\_define\_class(“Bar::Baz”), ruby has defined a class “Bar::Baz”  
and  
not a class Baz under Bar (what it look with Foo::Bar::Baz)

D\> The only way I could get it to work was to declare “Baz” under “Bar”.  
But is  
D\> that really the same thing?

In your case probably.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Daniel.Berger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Daniel.Berger](https://www.ruby-forum.com/u/Daniel.Berger)\
**Post date:** [November 22, 2005, 7:35pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704/3 "2005-11-22T19:35:22Z")

</div>

ts wrote:

> and not the call to rb\_define\_class()
> 
> With rb\_define\_class(“Bar::Baz”), ruby has defined a class “Bar::Baz” and  
> not a class Baz under Bar (what it look with Foo::Bar::Baz)
> 
> D\> The only way I could get it to work was to declare “Baz” under “Bar”. But is  
> D\> that really the same thing?
> 
> In your case probably.

What’s the difference between these two?

module Foo  
class Bar  
end  
class Bar::Baz  
end  
end

module Foo  
class Bar  
class Baz  
end  
end  
end

Is there any difference? What are the potential pitfalls, if any?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![Daniel.Berger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Daniel.Berger](https://www.ruby-forum.com/u/Daniel.Berger)\
**Post date:** [November 22, 2005, 8:07pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704/4 "2005-11-22T20:07:36Z")

</div>

On Wed, Nov 23, 2005 at 03:34:48AM +0900, Daniel B. wrote:

> What’s the difference between these two?

module Foo  
A = :Foo  
class Bar  
A = :Bar  
end  
class Bar::Baz  
A # =\> :Foo  
end  
end

module Foo  
class Bar  
class Baz  
A # =\> :Bar  
end  
end  
end

RUBY\_VERSION # =\> “1.8.3”

---

<div class="post-metadata">

**Author:** ![Daniel.Berger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Daniel.Berger](https://www.ruby-forum.com/u/Daniel.Berger)\
**Post date:** [November 22, 2005, 8:03pm UTC](https://www.ruby-forum.com/t/c-extensions-and-class-constants-nested-within-modules/52704/5 "2005-11-22T20:03:34Z")

</div>

Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > “Bar”. But is D\> that really the same thing?
> 
> module Foo  
> class Bar  
> class Baz  
> end  
> end  
> end
> 
> Is there any difference? What are the potential pitfalls, if any?

You could hit issues with constant lookup, but from a code point  
of view they should be the same.
