行,所以我有4个对象的像做分组在红宝石
=> [#<Graphic id: 3...">, #<Collection id: 1....">, #<Category id:...">, #<Volume id: 15...">]
matches.size
=> 4
与4个不同的对象(图形,收集,分类,体积)的阵列我现在需要这些分离成4的更简洁的方式基于对象的数组。所以我创造了这个方法,它的工作原理,但其所谓的hackish ....关于如何实现更简洁的方式同样的事情,任何想法...更多rubyesk
这里是我的方法
def self.get_results(matches)
graphics = [], collections = [], categories = [], sub_categories = []
matches.group_by(&:class).each do |key, group|
case group.first.class.to_s
when "Graphic"
graphics << group
when "Category"
categories << group
when "SubCategory"
sub_categories << group
when "Collection"
collections << group
end
end
[graphics.flatten, collections.flatten, categories.flatten, sub_categories.flatten]
end
注意,所谓“案件group.first”,然后选择“图形时,”应该是足够的,大小写没有更广泛的===,不是== – tokland 2011-01-22 20:13:29