Issue #8478 has been updated by marcandre (Marc-Andre Lafortune).

Assignee set to matz (Yukihiro Matsumoto)

Right, that the behavior is changing and that this could break existing code. It might have been an interesting idea at the time `group_by` was introduced, but I doubt that Matz will feel the change is compelling enough to break compatibility today.
----------------------------------------
Feature #8478: The hash returned by Enumerable#group_by should have an empty array for its default value
https://bugs.ruby-lang.org/issues/8478#change-39661

Author: phiggins (Pete Higgins)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 


Without this patch, nil checks might need to be done on the return value of Enumerable#group_by:

$ cat test_group_by.rb 
a = [1, 2, 3, "a", "b"]
g = a.group_by {|o| o.class }

puts "Fixnums: #{g[Fixnum].size}"
puts "Strings: #{g[String].size}"
puts "Arrays: #{g[Array].size}"

$ ruby test_group_by.rb 
Fixnums: 3
Strings: 2
test_group_by.rb:6:in `<main>': undefined method `size' for nil:NilClass (NoMethodError)

This patch adds a default value of an empty array to the hash returned by Enumerable#group_by, so the script above will work:

$ ./ruby -I.:lib test_group_by.rb 
Fixnums: 3
Strings: 2
Arrays: 0



-- 
http://bugs.ruby-lang.org/