Ask a Question related to Ruby, Design and Development.
-
Dmitry V. Sabanin #1
Invoking method with a block in C extension
Hi.
I'm converting some of my ruby code to C extension for better perfomance.
I ran into problems with executing methods that take blocks.
I.e. in ruby i have
string.gsub!(/regexp/) { |subst| ... }
and i can't find the way to translate it to C. I tried to search through
ruby-talk archives, but I couldn't find anything usefull.
As far as I was looking at problem, I found rb_iterate, rb_yield, but I
couldn't clearly understand what's their purpose, and how to get them all
together to achieve what I want.
Could somebody please explain me how can I implement this ruby code in C?
I'd really appreciate any help. Thanks in advance!
--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.
Dmitry V. Sabanin Guest
-
invoking component method slower than local function ?
Hello all, I am currently re-coding in object way an old application with linear code under coldfusion 6.1 MX. Components are ok but I encounter... -
invoking a method
Whenever I invoke a method on my web service I got the following error Client found response content type of 'text/html' Does somebody know... -
passing parameters to or invoking a method of an activex control in asp
I wrote a simple client/server chat program and the client runs as an activex control within an asp page. I have the users logging into the page... -
Passing a method block to a parent class
If I have **** CODE **** class A def method1( argument ) yield( argument ) end end -
"The system cannot find the file specified." when invoking method ~HTTP Get/Post
Developed and successfully tested my web service on a development server with .asmx mapped to v1.0.3705 \aspnet_isapi.dll, but when I migrated it... -
ts #2
Re: Invoking method with a block in C extension
>>>>> "D" == Dmitry V Sabanin <sdmitry@lrn.ru> writes:
D> As far as I was looking at problem, I found rb_iterate, rb_yield, but I
D> couldn't clearly understand what's their purpose, and how to get them all
D> together to achieve what I want.
Look in enum.c in the distribution of ruby, you have examples for call to
rb_iterate
Guy Decoux
ts Guest
-
Mauricio Fernández #3
Re: Invoking method with a block in C extension
--nFreZHaLTZJo0R7j
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
On Sat, Nov 22, 2003 at 10:34:27PM +0900, Dmitry V. Sabanin wrote:I was surprised to see that rb_funcall propagates the block passed with> Hi.
> I'm converting some of my ruby code to C extension for better perfomance.
> I ran into problems with executing methods that take blocks.
> I.e. in ruby i have
> string.gsub!(/regexp/) { |subst| ... }
> and i can't find the way to translate it to C. I tried to search through
> ruby-talk archives, but I couldn't find anything usefull.
> As far as I was looking at problem, I found rb_iterate, rb_yield, but I
> couldn't clearly understand what's their purpose, and how to get them all
> together to achieve what I want.
> Could somebody please explain me how can I implement this ruby code in C?
> I'd really appreciate any help. Thanks in advance!
rb_iterate, so there's 2 solutions (attached).
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Linux! Guerrilla UNIX Development Venimus, Vidimus, Dolavimus.
-- Mark A. Horton KA4YBR, [email]mah@ka4ybr.com[/email]
--nFreZHaLTZJo0R7j
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="extconf.rb"
require "mkmf"
create_makefile("gsub_example")
--nFreZHaLTZJo0R7j
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="gsub_example.c"
#include <ruby.h>
#include <stdio.h>
VALUE mCTest;
static
VALUE
block(VALUE args)
{
VALUE str = rb_ary_entry(args, 0);
Check_Type(str, T_STRING);
printf("Inside block, with string: %s.\n", RSTRING(str)->ptr);
return rb_funcall(str, rb_intern("upcase"), 0);
}
static
VALUE
block2(VALUE str)
{
Check_Type(str, T_STRING);
printf("Inside block, with string: %s.\n", RSTRING(str)->ptr);
return rb_funcall(str, rb_intern("upcase"), 0);
}
static
VALUE
foo(VALUE self, VALUE str, VALUE re)
{
VALUE proc;
proc = rb_proc_new(block, Qnil);
return rb_funcall(str, rb_intern("_gsub!"), 2, re, proc);
}
static
VALUE
call_gsub(VALUE args)
{
VALUE str, re;
str = rb_ary_entry(args, 0);
re = rb_ary_entry(args, 1);
return rb_funcall(str, rb_intern("gsub!"), 1, re);
}
static
VALUE
foo2(VALUE self, VALUE str, VALUE re)
{
VALUE args;
args = rb_ary_new();
rb_ary_push(args, str);
rb_ary_push(args, re);
return rb_iterate(call_gsub, args, block2, str);
}
void
Init_gsub_example()
{
mCTest = rb_define_module("CTest");
rb_define_singleton_method(mCTest, "foo", foo, 2);
rb_define_singleton_method(mCTest, "foo2", foo2, 2);
rb_eval_string("class String; def _gsub!(re,block); gsub!(re,&block) end end");
}
--nFreZHaLTZJo0R7j
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="tst.rb"
require "gsub_example"
a = "hello, world!"
CTest.foo(a, /[ehl]/)
p a
b = "hello, world!"
CTest.foo2(b, /[ehl]/)
p b
--nFreZHaLTZJo0R7j--
Mauricio Fernández Guest
-
Dmitry V. Sabanin #4
Re: Invoking method with a block in C extension
On Saturday 22 November 2003 21:55, Mauricio Fernández wrote:
Thank you very much, that code saved me :)> On Sat, Nov 22, 2003 at 10:34:27PM +0900, Dmitry V. Sabanin wrote:>> > Hi.
> > I'm converting some of my ruby code to C extension for better perfomance.
> > I ran into problems with executing methods that take blocks.
> > I.e. in ruby i have
> > string.gsub!(/regexp/) { |subst| ... }
> > and i can't find the way to translate it to C. I tried to search through
> > ruby-talk archives, but I couldn't find anything usefull.
> > As far as I was looking at problem, I found rb_iterate, rb_yield, but I
> > couldn't clearly understand what's their purpose, and how to get them all
> > together to achieve what I want.
> > Could somebody please explain me how can I implement this ruby code in C?
> > I'd really appreciate any help. Thanks in advance!
> I was surprised to see that rb_funcall propagates the block passed with
> rb_iterate, so there's 2 solutions (attached).
--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.
Dmitry V. Sabanin Guest



Reply With Quote

