Invoking method with a block in C extension

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. Passing a method block to a parent class
      If I have **** CODE **** class A def method1( argument ) yield( argument ) end end
    5. "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...
  3. #2

    Default 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

  4. #3

    Default 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:
    > 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).

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    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

  5. #4

    Default Re: Invoking method with a block in C extension

    On Saturday 22 November 2003 21:55, Mauricio Fernández wrote:
    > 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).
    Thank you very much, that code saved me :)

    --
    sdmitry -=- Dmitry V. Sabanin
    MuraveyLabs.


    Dmitry V. Sabanin Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139