C extensions and class constants nested within modules

Discussion in 'Ruby' started by Daniel Berger, Nov 22, 2005.

  1. 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.
    Daniel Berger, Nov 22, 2005
    #1
    1. Advertising

  2. Daniel Berger

    ts Guest

    >>>>> "D" == Daniel Berger <> 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
    ts, Nov 22, 2005
    #2
    1. Advertising

  3. ts wrote:
    >>>>>>"D" == Daniel Berger <> 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.


    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
    Daniel Berger, Nov 22, 2005
    #3
  4. On Wed, Nov 23, 2005 at 03:34:48AM +0900, Daniel Berger 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"

    --
    Mauricio Fernandez
    Mauricio Fernández, Nov 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,700
    Thomas Weidenfeller
    Oct 12, 2005
  2. Tobiah
    Replies:
    2
    Views:
    303
    Tobiah
    Sep 14, 2003
  3. Ultrus
    Replies:
    3
    Views:
    361
    Stefan Behnel
    Jul 9, 2007
  4. Martin DeMello
    Replies:
    3
    Views:
    114
    Joel VanderWerf
    Nov 9, 2006
  5. Dale
    Replies:
    1
    Views:
    109
Loading...

Share This Page