行,所以我有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