【问题标题】:How to call a C# IEnumerable from IronRuby如何从 IronRuby 调用 C# IEnumerable
【发布时间】:2011-05-23 22:06:56
【问题描述】:

我有一个 C# 方法:

  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);
      }
    }
  }

我可以在 IronPython 中这样称呼它:

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

但我不知道如何从 IronRuby 调用它:

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

我得到的只是Graphics+&lt;getPixels&gt;c__Iterator0.

我需要做什么才能在 IronRuby 中实际获取每个像素并对其进行处理?

【问题讨论】:

  • 我不能直接回答你的问题,但是感知灰度的计算有一个不准确之处:它应该是 rgb 和 0.3、0.59、0.11 的点积。人们对不同颜色的亮度有不同的感知。如果您希望表示颜色的感知亮度,所有这些都适用。
  • 你是对的:这不是一个直接的答案。甚至不是间接答案:)人们对 RGB 亮度的感知不均也是正确的,并且 RGB 权重有许多可能的值来制作更令人愉悦的灰度图像。但这与手头的问题无关(事实上,代码制作了完美的灰度图像)。现在我需要弄清楚如何在 IronRuby 中调用生成器...

标签: c# ruby ienumerable ironruby


【解决方案1】:

来自 Jimmy Schementi:

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

如果将 getPixels 的返回类型更改为 IEnumerable,则可以:

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

并且可以说它应该是 IEnumerable 而不是 IEnumerator,作为 IEnumerable.GetEnumerator() 给你一个 IEnumerator。

您的代码示例将一个闭包传递给 getPixels 方法,该方法只是 被忽略(所有 Ruby 方法在语法上都接受块/闭包, 并且他们可以选择使用它),并返回 IEnumerator。

IronRuby 今天不支持将 Ruby 的 Enumerable 模块映射到 IEnumerator 对象,因为返回 IEnumerator 有点尴尬 而不是来自公共 API 的 IEnumerable,但由于 IronPython 设置支持它的优先级,我们应该研究它。打开 http://ironruby.codeplex.com/workitem/6154.

~吉米

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多