Project

General

Profile

Actions

Feature #8478

open

The hash returned by Enumerable#group_by should have an empty array for its default value

Added by phiggins (Pete Higgins) over 11 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:55260]

Description

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


Files

group_by_default_empty_array.diff (1.81 KB) group_by_default_empty_array.diff phiggins (Pete Higgins), 06/03/2013 04:37 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0