Created
September 2, 2009 17:14
-
-
Save shelling/179835 to your computer and use it in GitHub Desktop.
Ruby Extension in "hello, world!"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
$:.unshift "example.ext" | |
require "example" | |
puts Example.new.just_hello | |
puts Example.new.hello("shelling") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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