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 6070553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:56:47+00:00 2026-05-23T09:56:47+00:00

If I have Foo::Bar written in Ruby, and I want to add a method

  • 0

If I have Foo::Bar written in Ruby, and I want to add a method to Bar as a C extension. Right now when I create Foo::Bar in C like this:

static VALUE Foo;
static VALUE Bar;

static VALUE 
print_string(VALUE self, VALUE string) {
  printf("%s", StringValuePtr(string));
  return Qnil;
}

void Init_foo() {
    Foo = rb_define_module("Foo");
    Bar = rb_define_class_under(Foo, "Bar", rb_cObject);
    rb_define_method(Bar, "print_string", print_string, 1);
}

But the problem is:

ruby-1.9.2-p180 :001 > require 'ext/foo'   #=> ["Foo"]
ruby-1.9.2-p180 :002 > f = Foo::Bar.new   #=> #<Foo::Bar:0x000001046bce48>
ruby-1.9.2-p180 :003 > f.original_ruby_method
NoMethodError: undefined method `original_ruby_method' for #<Foo::Bar:0x000001046bce48>

So I am essentially overwriting the original Foo::Bar. How do I extend it and not overwrite it?

  • 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-23T09:56:48+00:00Added an answer on May 23, 2026 at 9:56 am

    An alternative to your solution, where you require your Ruby code from your C extension, would be to require the extension from the Ruby code.

    Add require 'foo.so' or require 'ext/foo.so' (depending on where your compiled library ends up) to lib/foo.rb, then in the client code just call require 'foo' as normal (assuming lib is on your load path).

    I think doing it this way round is clearer and more common.

    Note that you can use the .so suffix even if your platform produces something else, i.e. it’ll still work on a mac when the actual file is a .bundle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a value like this: Foo Bar Another Value something else What regex
I have two classes, Foo and Bar, that have constructors like this: class Foo
I've noticed that sometimes, C macros are written as something like this: #define foo(bar)
I have this: SolutionName: Foo.sln Assembly: Foo.Bar Namespaces are: Foo.Bar.Views Foo.Bar.Model Foo.Bar.BusinessObjects Foo.Bar.Services Should
it appears if you have something like var my_var = {foo : bar}; in
If I have a comma separated file like the following: foo,bar,n ,a,bc,d one,two,three ,a,bc,d
Imagine I have this object (written with Ruby literals) stored in a MongoDB: {tags
If I want to create: <foo> <bar>example1</bar> <bar>example2</bar> </foo> using e4x, and I already
I have an enormous list of the following items: _tags.Add(Foo.Bar, new Baz( ... ));
I have a batch foo.bar in a directory d:\progs\ . In this directory I

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.