Project

General

Profile

Bug #19448 ยป bug.rb

Also contains a workaround - bobanj (Boban Jovanoski), 02/17/2023 03:39 PM

 
# frozen_string_literal: true

require 'set'

# Bug
h = Hash.new(Set.new)
h[:a] << 1
h[:b] << 2
puts "keys: #{h.keys}"
puts "h[:a]: #{h[:a]}"
puts "h[:b]: #{h[:b]}"
puts 'Expecting to see :a and :b in list of keys'

# Workaround
puts 'With workaround'
h = Hash.new { |hash, key| hash[key] = Set.new }
h[:a] << 1
h[:b] << 2
puts "keys: #{h.keys}"
puts 'Keys are displayed correctly'
    (1-1/1)