------art_8853_29012450.1157696619884
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Thanks a bundle.
Tested Successfully.

Saumya.


On 9/6/06, Rick DeNatale <rick.denatale / gmail.com> wrote:
>
> On 9/6/06, Saumya Dikshit <saumzyster / gmail.com> wrote:
> > Just to clarify, "rb_load_file" and "ruby_run" will execute the ruby
> script.
> > But incase, there are more than one singleon methods defined in the
> script
> > and I want to call only one of them.
> >
> > If I have a "test.rb"
> >
> > def good
> >   puts 'I am gud'
> > end
> >
> > def bad
> >   puts 'I am bad'
> > end
>
> > I want to include one of these methods from C code.
> > Since executing a script in this case will simply
> > Do nothing as I have defined singletons here.
> > How do I acheve It.
>
> I beleive that this will work, none of this actually tested.
>
> First, they're not singleton methods, methods defined at the top level
> are private instance methods of class Object.  So you need to have a
> receiver, but any object will do, say nil.  As far as I can tell the
> rb_funcall function doesn't check whether or not a method is private,
> but this might change in future versions of ruby.
>
> So something like this might work to call the good method.
>
>      rb_funcall(Qnil, rb_intern("good"), 0)
>
> if you instead made these class methods of a class:
>
> class TestClass
>     def TestClass.good
>            puts 'I am gud'
>      end
>
>      def TestClass.bad
>            puts 'I am bad'
>      end
> end
>
> Then in your C code you could do something like this (again untested):
>
>      VALUE test_class  b_const_get(rb_cObject, rb_intern("TestClass"));
>       rb_funcall(test_class, rb_intern("good"), 0, 0)
>
> The last two arguments to rb_funcall are the number of arguments and
> an array of the VALUEs of the arguments, since the count is 0 we can
> use 0 for the array.
>
> And if you made the methods instance methods:
>
> class TestClass
>     def good
>            puts 'I am gud'
>      end
>
>      def bad
>            puts 'I am bad'
>      end
> end
>
> Then your C code might look something like this (once again untested):
>
>      VALUE test_class  b_const_get(rb_cObject, rb_intern("TestClass"));
>      VALUE test_instance  b_class_new_instance(0,0,test_class)
>       rb_funcall(test_instance, rb_intern("good"), 0, 0)
>
> similarly here, the first two arguments of rb_class_new_instance are
> the argument count and argument array for the initialize method.
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
>
>

------art_8853_29012450.1157696619884--