Skip to content

Instantly share code, notes, and snippets.

@adamhunter
Created February 26, 2013 19:00
Show Gist options
  • Select an option

  • Save adamhunter/5041075 to your computer and use it in GitHub Desktop.

Select an option

Save adamhunter/5041075 to your computer and use it in GitHub Desktop.

Revisions

  1. adamhunter created this gist Feb 26, 2013.
    7 changes: 7 additions & 0 deletions extconf.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    require 'mkmf'

    have_header('ruby.h') or missing('ruby.h')

    dir_config("ivar")

    create_makefile("ivar")
    1 change: 1 addition & 0 deletions irb.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ruby extconf.rb && make && irb -I . -r ivar
    19 changes: 19 additions & 0 deletions ivar.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #include "ruby.h"

    static VALUE rb_mIvar;

    static VALUE rb_ivar_iv_get(VALUE self, VALUE key) {
    return rb_ivar_get(self, rb_to_id(key));
    }

    static VALUE rb_ivar_iv_set(VALUE self, VALUE key, VALUE value) {
    return rb_ivar_set(self, rb_to_id(key), value);
    }

    void Init_ivar() {

    rb_mIvar = rb_define_module("Ivar");
    rb_define_method(rb_mIvar, "ivar_get", rb_ivar_iv_get, 1);
    rb_define_method(rb_mIvar, "ivar_set", rb_ivar_iv_set, 2);

    }
    12 changes: 12 additions & 0 deletions usage.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    $:.unshift('.')
    require 'ivar'

    Object.send(:include, Ivar)

    e = StandardError.new("foo")

    puts "Error message is: #{e.message}"

    e.ivar_set(:mesg, "bar")

    puts "Error message is: #{e.message}"