我有以下代码:
class A
end
class B
end
a1 = A.new
a2 = A.new
b1 = B.new
b2 = B.new
array = [a1, a2, b1, b2]
hash = {}
array.each do |obj|
if hash[obj.class] = nil
hash[obj.class] = []
else
hash[obj.class] << obj
end
end
我希望哈希等于
{ A => [a1,a2], B => [b1,b2] }
但它告诉我我不能使用<<
运算符。