Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:06:44+00:00 2026-05-29T16:06:44+00:00

I am trying to define a class inside a module with the Ruby C

  • 0

I am trying to define a class inside a module with the Ruby C API. However, the way I have seen this done all over the net doesn’t seem to work for me. Specifically, the top-level module gets created but the class can’t be found inside the module. Here’s my C file:

#include <ruby.h>

static VALUE mTree;
static VALUE cNode;

VALUE hello_world(VALUE klass)
{
    return rb_str_new2("hello world");
}

void Init_tree()
{
  mTree = rb_define_module("Tree");
  cNode = rb_define_class_under(mTree, "Node", rb_cObject); 
  rb_define_method(cNode, "hello_world", hello_world, 0);
}

Here’s my extconf.rb:

require 'mkmf'
create_makefile('tree')

Here’s my test script:

require 'tree'
puts Tree        # => Tree
puts Tree::Node  # => uninitialized constant Tree::Node (NameError)

Can anybody help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:06:45+00:00Added an answer on May 29, 2026 at 4:06 pm

    This is strange, your example works for me:

    → ruby extconf.rb     
    creating Makefile
    → make          
    linking shared-object tree.bundle
    → irb
    >> $:<<'.'
    => [...]
    >> require 'tree'
    => true
    >> Tree
    => Tree
    >> Tree.class
    => Module
    >> Tree::Node.class
    => Class
    >> Tree::Node.new.hello_world
    => "hello world"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that attr_accessor is defined inside the Module class, but I am trying
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I'm stuck. I'm trying to dynamically define a class method and I can't wrap
I'm trying to generate a CSS class that will help me define a page
I'm trying to create a point class which defines a property called coordinate. However,
I have the following members defined in a class that I'm trying to deserialise:
I have a big C++ class, which includes 5 other classes inside, and some
Normally, I've seen prototype functions declared outside the class definition, like this: function Container(param)
I have written an F# module that has a list inside: module MyModule type
I have been trying to implement my own linked list class for didactic purposes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.