# How to iterate through a C# IEnumerable from IronRuby?

**URL:** <https://www.ruby-forum.com/t/how-to-iterate-through-a-c-ienumerable-from-ironruby/207348>\
**Category:** IronRuby\
**Created:** [May 24, 2011, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-iterate-through-a-c-ienumerable-from-ironruby/207348 "2011-05-24T15:29:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 24, 2011, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-iterate-through-a-c-ienumerable-from-ironruby/207348/1 "2011-05-24T15:29:16Z")

</div>

Apologies if this is a basic Ruby question. I have a C# method:

public class Myro {  
…  
public static IEnumerator getPixels(Picture picture) {  
for (int x=0; x \< picture.width; x++) {  
for (int y=0; y \< picture.height; y++) {  
yield return picture.getPixel(x, y);  
}  
}  
}  
}

I can call this fine in IronPython:

for pixel in getPixels(pic):  
r, g, b = getRGB(pixel)  
gray = (r + g + b)/3  
setRGB(pixel, gray, gray, gray)

But I don’t see how to call this from IronRuby:

Myro::getPixels(pic) do |pixel|  
r, g, b = Myro::getRGB pixel  
gray = (r + g + b)/3  
Myro::setRGB(pixel, gray, gray, gray)  
end

All I get back is Myro+c\_\_Iterator0.

What do I need to do to actually get each pixel in IronRuby and  
process it? Do I need an extension method, perhaps called “each”? If  
so, can someone point me to some code?

Thanks in advance for any help,

-Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [May 24, 2011, 6:28pm UTC](https://www.ruby-forum.com/t/how-to-iterate-through-a-c-ienumerable-from-ironruby/207348/2 "2011-05-24T18:28:39Z")

</div>

Doug,

If you change the return type of getPixels to IEnumerable, then this  
works:

```
Myro::getPixels(pic).each do |pixel|
    ...
end

```

And arguably it should be IEnumerable rather than IEnumerator, as an  
IEnumerable.GetEnumerator() gives you an IEnumerator.

Your code example passes a closure to the getPixels method, which just  
gets ignored (all Ruby methods syntactically accept a block/closure,  
and they can choose to use it), and returns the IEnumerator.

IronRuby today doesn’t support mapping Ruby’s Enumerable module to  
IEnumerator objects, since it’s kind awkward to return an IEnumerator  
rather than an IEnumerable from a public API, but since IronPython  
sets a precedence for supporting it we should look into it. Opened  
[http://ironruby.codeplex.com/workitem/6154](http://ironruby.codeplex.com/workitem/6154).

~Jimmy
