Skip to content

Instantly share code, notes, and snippets.

@shelling
Created September 2, 2009 17:14
Show Gist options
  • Save shelling/179835 to your computer and use it in GitHub Desktop.
Save shelling/179835 to your computer and use it in GitHub Desktop.
Ruby Extension in "hello, world!"
#!/usr/bin/env ruby
$:.unshift "example.ext"
require "example"
puts Example.new.just_hello
puts Example.new.hello("shelling")
#include <stdio.h>
#include <ruby.h>
static VALUE cExample;
static VALUE
just_hello (VALUE object)
{
return rb_str_new2("hello");
}
static VALUE
hello (VALUE object, VALUE string)
{
VALUE arr = rb_str_new2("hello, ");
rb_str_concat(arr, string);
rb_str_concat(arr, rb_str_new2("!"));
return arr;
}
void
Init_example ()
{
cExample = rb_define_class("Example", rb_cObject);
rb_define_method(cExample, "just_hello", just_hello, 0);
rb_define_method(cExample, "hello", hello, 1);
}
require "mkmf"
dir_config "example"
create_makefile "example"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment