【问题标题】:C#'s LINQ for collection manipulation equivalent in rubyC# 的 LINQ 用于 ruby​​ 中的集合操作等价物
【发布时间】:2011-10-03 18:12:52
【问题描述】:

我是 ruby​​ 开发的新手,我目前正在使用 rails 2.3.11 在 ruby​​ 1.8.7 中开发一个项目,我想知道这种语言中是否有与 C# 的 linq 等价的集合操作,例如 where 子句。

谢谢。

【问题讨论】:

    标签: c# ruby-on-rails ruby linq collections


    【解决方案1】:

    Ruby 中 Linq 的 where 等价于 find_all

    查看documentation for the Enumerable Module 了解其他功能。

    【讨论】:

      【解决方案2】:

      命名范围可能是您正在寻找的......我认为 Rails 3.1 也有一些更深入的“位置”之类的东西。

      【讨论】:

        【解决方案3】:

        你可以做与 linq 中类似的事情,但语法是相当不同的。 你会这样写:

        a.select { |i|
            (do something with item i and decide whether 
             it should be added to the output array 
             or not by returning true or false)
        }
        

        假设 a 是一个数组,此代码将返回一个新数组,其中包含通过验证码的元素。

        Documentation of the array class

        【讨论】:

          猜你喜欢
          • 2011-01-21
          • 1970-01-01
          • 1970-01-01
          • 2014-01-07
          • 2019-07-15
          • 2012-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多