我正在玩Ruby on Codecademy,不知道如何进一步减少这些。当前的代码:在Ruby中的多个对象中使用枚举数
group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]
group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]
group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]
over_4_feet = Proc.new { |height| height >= 4 }
can_ride_1 = group_1.select(&over_4_feet)
can_ride_2 = group_2.select(&over_4_feet)
can_ride_3 = group_3.select(&over_4_feet)
我不知道我怎么会去这样的事情:
can_ride_(1..3).each {|x| group_(x).select(&over_4_feet)}
是否有可能,当对象保存重复的图案像这样做,以这种方式使用可枚举的方法?我不介意它是否是正则表达式,但对可能推荐的模式感到好奇。
有你看着。发送http://stackoverflow.com/questions/5349624/how-to-call-methods-dynamically-based-on-their-name – mahatmanich
我觉得动态调度是你是什么寻找... – mahatmanich
@mahatmanich不会'group _#{x}'工作?我记得在某处看到这个'#' – niceman