getting a method from a block in C

Discussion in 'Ruby' started by elathan@phys.uoa.gr, Dec 12, 2003.

  1. Guest

    Hello!

    Suppose I have some ruby code:

    def bar
    puts "Hello"
    end

    foo { bar }

    Now, foo is part of a Ruby extension, but bar is user implemented.
    The C implementation of foo can use rb_block_given_p() to identify if
    it was called with a block. My question is: can I grab the bar method, save
    its pointer somewhere and call it from other places in my code? I.e. is
    there an option in the Ruby C API to parse/identify args in a given block?

    I don't want to yield bar, just save it and call it later (foo is actually a C++
    constructor, which takes as an argument a pointer to a user function).


    Regards,
    --
    Elias
     
    , Dec 12, 2003
    #1
    1. Advertising

  2. ts Guest

    >>>>> "e" == elathan <> writes:

    e> I don't want to yield bar, just save it and call it later (foo is
    e> actually a C++ constructor, which takes as an argument a pointer to a
    e> user function).

    if (rb_block_given_p()) {
    proc = rb_block_proc();
    }

    when you want to call it

    rb_funcall(proc, rb_intern("call"), ...);

    don't forget to define a mark function, and call rb_gc_mark(proc)


    p.s. : for 1.6 this is rb_f_lambda() but this function is deprecated in 1.8


    Guy Decoux
     
    ts, Dec 12, 2003
    #2
    1. Advertising

  3. Guest

    Quoting ts <>:
    >
    > if (rb_block_given_p()) {
    > proc = rb_block_proc();
    > }
    >
    > when you want to call it
    >
    > rb_funcall(proc, rb_intern("call"), ...);


    Ah nice! That is what I wanted. Thanks a lot.

    > don't forget to define a mark function, and call rb_gc_mark(proc)


    You mean a mark function for the Ruby garbage collector?

    Regards,
    --
     
    , Dec 12, 2003
    #3
  4. ts Guest

    >>>>> "e" == elathan <> writes:

    >> don't forget to define a mark function, and call rb_gc_mark(proc)


    e> You mean a mark function for the Ruby garbage collector?

    yes, the block must be marked otherwise the ruby GC will remove it.

    This mean that you must define a mark function for your C++ object and
    call rb_gc_mark(proc) inside it.


    Guy Decoux
     
    ts, Dec 12, 2003
    #4
  5. Hello Guy!

    On Fri, Dec 12, 2003 at 07:51:30PM +0900, ts wrote:
    > >>>>> "e" == elathan <> writes:

    >
    > >> don't forget to define a mark function, and call rb_gc_mark(proc)

    >
    > e> You mean a mark function for the Ruby garbage collector?
    >
    > yes, the block must be marked otherwise the ruby GC will remove it.
    >
    > This mean that you must define a mark function for your C++ object and
    > call rb_gc_mark(proc) inside it.


    I think I have done what you suggested. However I get:

    foo.rb:42: [BUG] rb_gc_mark(): unknown data type 0x28(0x89963f8) non object
    ruby 1.8.0 (2003-08-04) [i586-linux]

    Which means that I haven't done it in the right way.

    I tried using gdb to identify the 'non-object' variable which can't be
    marked, with no luck. Is there another way to accomplish this?

    Also, the only way to associate a mark function with a new ruby class
    is via Data_Wrap_Struct/Data_Make_Struct. Is this correct?

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky
     
    Elias Athanasopoulos, Dec 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    1
    Views:
    723
    Showjumper
    Mar 19, 2005
  2. Noozer

    Block DIV within a block DIV?

    Noozer, Jan 6, 2005, in forum: HTML
    Replies:
    3
    Views:
    11,438
    Mitja
    Jan 6, 2005
  3. morrell
    Replies:
    1
    Views:
    1,003
    roy axenov
    Oct 10, 2006
  4. John J. Franey
    Replies:
    3
    Views:
    142
    George
    Jun 15, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    230
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page