How to call C # IEnumerable from IronRuby

I have a C # method:

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 it 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 what to call it 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 is Graphics+<getPixels>c__Iterator0.

What do I need to do to get every pixel in IronRuby and process it?

+4
source share
1 answer

From Jimmy Schementi:

http://rubyforge.org/pipermail/ironruby-core/2011-May/007982.html

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

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

And perhaps it should be IEnumerable, not IEnumerator, as IEnumerable.GetEnumerator () provides you with IEnumerator.

An example of your code passes the closure of the getPixels method, which simply gets ignored (all Ruby methods syntactically accept a block / closure, and they can use it), and returns an IEnumerator.

IronRuby today does not support the display of the Ruby Enumerable module for IEnumerator Objects, since it is embarrassing to return IEnumerator and not IEnumerable from the public API, but since IronPython sets the priority for its support, we should study it. open http://ironruby.codeplex.com/workitem/6154 .

~ Jimmy

+3
source

All Articles