在Ruby on Rails中重构模型方法

Chr*_*ier 4 ruby methods refactoring ruby-on-rails

我的阵营在铁轨中使用的常用习语如下:

def right_things(all_things, value)
    things = []
    for thing in all_things
       things << thing if thing.attribute == value
    end
    return things
end
Run Code Online (Sandbox Code Playgroud)

我怎样才能让这更好/更快/更强?

谢谢

-C

Chr*_*ett 13

def right_things(all_things, value)
    all_things.select{|x| x.attribute == value}
end
Run Code Online (Sandbox Code Playgroud)