The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Wrapping a C Function as a Block

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Wrapping a C Function as a Block Posted: Jan 19, 2007 9:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Wrapping a C Function as a Block
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

I had previously been using rb_iterate and a call to Proc.new to build a block in C. But that takes two function calls while the undocumented rb_proc_new is just one.

 require 'rubygems'
 require 'inline'
 module Kernel
 inline do |ext|
 ext.c_raw %{
 static VALUE sum(VALUE args)
 {
 return rb_funcall(rb_ary_entry(args, 0), rb_intern("+"), 1, 
 rb_ary_entry(args, 1));
 return Qnil;
 }
 }
 ext.c %{
 VALUE sum_block()
 {
 return rb_proc_new(sum, 0);
 }
 }
 end
 end
 p sum_block[13, 2]
 p (0..20).inject(0, &sum_block)

Most of the time rb_iterate is what you need. But, you know, in case you actually want a Proc object.

Read: Wrapping a C Function as a Block

Topic: Why do people want to program in Ruby anyhow? Previous Topic   Next Topic Topic: Camping's Youthfulness Wins Annual Award

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use