Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我们没有 group_by 有什么原因吗?可枚举模块中的方法。为什么我必须这样做:
something = something.group_by(&:attr)
对比
something.group_by!(&:attr)
因为 group_by 可以改变变量的类型(而变量方法不能这样做)。
例如:
something = [1,2,3,4,4] # something is Array something = something.group_by{|x| x % 2 } # => {1=>[1, 3], 0=>[2, 4, 4]} # now something is a Hash