From: EMOTO Masahiko Date: 2003-09-21T23:59:40+09:00 Subject: [ruby-list:38416] モジュール内で別のモジュールのオブジェクトを返すには? 現在 C 言語でクラスを作成しています。 クラス Foo とクラス Bar があって、クラス Bar 内のメソッド foobar() の戻り値として Foo のオブジェクトを新たに作って返したい のですがうまく動きません。 irb の中で Bar.foobar() を実行すると、 NotImplementedError: method call on terminated object というエラーが返ってきます。 どのようにすれば良いのでしょうか? 簡略化したサンプルをつけようと思ったのですが、 この例では動いてしまっています。だだし、やっている ことはこのようなことです。 環境: OS Linux / Cygwin Ruby 1.6.8 江本 ================= foo.c ================ #include #include static VALUE cFoo; typedef struct foo_s { } foo_t; VALUE get_Foo () { return cFoo; } void free_Foo(foo_t *p) { fprintf(stderr,"free_Foo\n"); } VALUE new_Foo(int argc, VALUE* argv, VALUE klass) { VALUE obj; foo_t* p; fprintf(stderr,"new_Foo\n"); printf("klass=%d\n",klass); obj = Data_Wrap_Struct(klass,0,free_Foo,0); rb_obj_call_init(obj,argc,argv); return obj; } VALUE func_Foo(VALUE self) { fprintf(stderr,"func_Foo\n"); return self; } VALUE initialize_Foo(int argc, VALUE* argv, VALUE* obj) { fprintf(stderr,"initialize_Foo\n"); } Init_foo () { fprintf(stderr,"Init_foo\n"); cFoo = rb_define_class("Foo",rb_cObject); printf("cFoo=%d\n",cFoo); rb_define_singleton_method(cFoo,"new",new_Foo,-1); rb_define_method(cFoo,"initialize",initialize_Foo,-1); rb_define_method(cFoo,"func",func_Foo,0); } ================= foo.c ================ ================= bar.c ================ #include #include extern VALUE new_Foo(int argc,VALUE* argv,VALUE klass); extern VALUE get_Foo(); VALUE cBar; typedef struct bar_s { } bar_t; void free_Bar(bar_t *p) { fprintf(stderr,"free_Bar\n"); } VALUE new_Bar(int argc, VALUE* argv, VALUE* klass) { VALUE obj; bar_t* p; fprintf(stderr,"new_Bar\n"); obj = Data_Wrap_Struct(klass,0,free_Bar,0); rb_obj_call_init(obj,argc,argv); return obj; } VALUE initialize_Bar(int argc, VALUE* argv, VALUE* obj) { fprintf(stderr,"initialize_Bar\n"); } VALUE createFoo() { VALUE obj; VALUE cFoo; fprintf(stderr,"createFoo\n"); cFoo = get_Foo(); obj = new_Foo(0,0,cFoo); return obj; } Init_bar () { fprintf(stderr,"Init_bar\n"); rb_require("foo.so"); cBar = rb_define_class("Bar",rb_cObject); rb_define_singleton_method(cBar,"new",new_Bar,-1); rb_define_method(cBar,"initialize",initialize_Bar,-1); rb_define_method(cBar,"createFoo",createFoo,0); } ================= bar.c ===========