# Rb\_iterate

**URL:** <https://www.ruby-forum.com/t/rb-iterate/78654>\
**Category:** Ruby\
**Created:** [December 7, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/rb-iterate/78654 "2006-12-07T16:53:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [December 7, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/rb-iterate/78654/1 "2006-12-07T16:53:24Z")

</div>

If anyone out there has used the “rb\_iterate” function in a Ruby C  
extension, could you please post a simple explanation of (a) how to  
use the function and (b) an explanation of the method signature?

From eval.c …

VALUE  
rb\_iterate(it\_proc, data1, bl\_proc, data2)  
VALUE (\*it\_proc) \_((VALUE)), (\*bl\_proc)(ANYARGS);  
VALUE data1, data2;  
{  
…  
}

I understand that “it\_proc” and “bl\_proc” are function pointers and  
“data1” and “data2” are the arguments that are passed to each  
function, respectively. What I don’t understand is how all these work  
together to iterate over a collection.

Pointers, tips, tutorials, explanations?

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [December 9, 2006, 10:03am UTC](https://www.ruby-forum.com/t/rb-iterate/78654/2 "2006-12-09T10:03:05Z")

</div>

On 12/8/06, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If anyone out there has used the “rb\_iterate” function in a Ruby C  
> extension, could you please post a simple explanation of (a) how to  
> use the function and (b) an explanation of the method signature?

From README.EXT:

\*\* Control Structure

VALUE rb\_iterate(VALUE (\*func1)(), void \*arg1, VALUE (\*func2)(), void  
\*arg2)

Calls the function func1, supplying func2 as the block. func1 will be  
called with the argument arg1. func2 receives the value from yield as  
the first argument, arg2 as the second argument.

VALUE rb\_yield(VALUE val)

Evaluates the block with value val.

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [December 7, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/rb-iterate/78654/3 "2006-12-07T17:45:48Z")

</div>

On Fri, 8 Dec 2006, Tim P. wrote:

> {  
> …  
> }
> 
> I understand that “it\_proc” and “bl\_proc” are function pointers and  
> “data1” and “data2” are the arguments that are passed to each  
> function, respectively. What I don’t understand is how all these work  
> together to iterate over a collection.
> 
> Pointers, tips, tutorials, explanations?

see ext/dbm/dbm.c

at a glance at this

492 static VALUE  
493 fdbm\_replace(obj, other)  
494 VALUE obj, other;  
495 {  
496 fdbm\_clear(obj);  
497 rb\_iterate(each\_pair, other, update\_i, obj);  
498 return obj;  
499 }

this

463 static VALUE  
464 each\_pair(obj)  
465 VALUE obj;  
466 {  
467 return rb\_funcall(obj, rb\_intern(“each\_pair”), 0, 0);  
468 }

this

472 static VALUE  
473 update\_i(pair, dbm)  
474 VALUE pair, dbm;  
475 {  
476 Check\_Type(pair, T\_ARRAY);  
477 if (RARRAY(pair)-\>len \< 2) {  
478 rb\_raise(rb\_eArgError, “pair must be [key, value]”);  
479 }  
480 fdbm\_store(dbm, RARRAY(pair)-\>ptr[0], RARRAY(pair)-\>ptr[1]);  
481 return Qnil;  
482 }

and this

795 rb\_define\_method(rb\_cDBM,“replace”, fdbm\_replace, 1);

i think we can translate

497 rb\_iterate(each\_pair, other, update\_i, obj);

which would look like

dbm.replace other

in ruby

as

iterate over ‘other’ via the method ‘each\_pair’ and, for each yielded  
pair,  
call the method ‘update\_i’ on ‘obj’.

at least that what i’m guessing - i have **no** idea 😉

regards.

-a
