[C exte] rb_protect() with more than a single parameters

Hi, rb_protect() is declared as:

  VALUE
  rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state)

Ok, so if from C I want to call a Ruby method passing N VALUE
parameters, should I really create a Ruby Array and pass it as _data_
in rb_protect() ?? really? is there any other way?

Thanks a lot.

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

you could use something like this:

struct yourdatatype
{

};

yourdatatype val;

rb_protect(...,(VALUE)&yourdatatype, ... )

···

--
Posted via http://www.ruby-forum.com/.

I did not find another way after searching for a while, including the
source of all the extensions I could get my hands on. I was a little
surprised too.

I ended up writing my own wrappers around rb_funcall and rb_funcall2, using
rb_rescue2. Here's a gist:

  Examples of wrapping rb_funcall and rb_funcall2 with rb_rescue2. · GitHub

These come from a program that embeds ruby, not an extension, so they were
written to suit those needs. For example, rescuing rb_eException might be
too broad, and registering/unregistering args with the GC might not be
necessary for all applications. I think this is most probably the reason
why ruby does not provide such functions.

In any event, I've been meaning to put these snippets out there in a
related thread in hope they might help someone. I hope they help you in
some way. I would be very interested in any feedback you might have.

Regards,
Ammar

···

On Thu, May 24, 2012 at 8:59 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Ok, so if from C I want to call a Ruby method passing N VALUE
parameters, should I really create a Ruby Array and pass it as _data_
in rb_protect() ?? really? is there any other way?

anyone can give me a hand with this pls
http://pastie.org/3964924

Really interesting your approach :slight_smile:

Thanks a lot to both.

···

2012/5/25 Ammar Ali <ammarabuali@gmail.com>:

On Thu, May 24, 2012 at 8:59 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Ok, so if from C I want to call a Ruby method passing N VALUE
parameters, should I really create a Ruby Array and pass it as _data_
in rb_protect() ?? really? is there any other way?

I did not find another way after searching for a while, including the source
of all the extensions I could get my hands on. I was a little surprised too.

I ended up writing my own wrappers around rb_funcall and rb_funcall2, using
rb_rescue2. Here's a gist:

Examples of wrapping rb_funcall and rb_funcall2 with rb_rescue2. · GitHub

These come from a program that embeds ruby, not an extension, so they were
written to suit those needs. For example, rescuing rb_eException might be
too broad, and registering/unregistering args with the GC might not be
necessary for all applications. I think this is most probably the reason why
ruby does not provide such functions.

In any event, I've been meaning to put these snippets out there in a related
thread in hope they might help someone. I hope they help you in some way. I
would be very interested in any feedback you might have.

Regards,
Ammar

--
Iñaki Baz Castillo
<ibc@aliax.net>

You are comparing the values as strings (
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-3C-3D-3E\). You need
to convert the values to floats, by changing

p data[0].map {|f| f['price']}.flatten.max

to

p data[0].map {|f| f['price'].to_f}.flatten.max

(although the flatten seems unnecessary).

Alternatively:

* use the CSV library's numeric converter to convert the fields when they
are read
* don't use CSV library if the data is this simple

--pcs

···

On Fri, May 25, 2012 at 12:06 AM, Ivan Vilches Basaul < ivan_vilches@hotmail.com> wrote:

anyone can give me a hand with this pls
http://pastie.org/3964924

as paul have said, data is being treated as text. since you are using
csv lib, use the :converters option.. on your case, :converters=>:all
is good enough.

best regards -botp

···

On Fri, May 25, 2012 at 3:06 PM, Ivan Vilches Basaul <ivan_vilches@hotmail.com> wrote:

anyone can give me a hand with this pls
http://pastie.org/3964924