aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2020-12-17 14:43:11 -0500
committerMarc-André Lafortune <github@marc-andre.ca>2020-12-19 03:59:51 -0500
commit0d3dc2ec807c313d0952d15ac4f30bc8586bba2f (patch)
tree622a4359e28f7fe1021dac07708749a90856f209
parent5611066e03fe73bdbb08cc46f79530c69975cf17 (diff)
downloadruby-0d3dc2ec807c313d0952d15ac4f30bc8586bba2f.tar.gz
Make `Hash#except` always return a Hash
[Feature #15822]
-rw-r--r--hash.c3
-rw-r--r--spec/ruby/core/hash/except_spec.rb7
2 files changed, 6 insertions, 4 deletions
diff --git a/hash.c b/hash.c
index 2f53c24647..9b4c315a09 100644
--- a/hash.c
+++ b/hash.c
@@ -2643,7 +2643,8 @@ rb_hash_except(int argc, VALUE *argv, VALUE hash)
int i;
VALUE key, result;
- result = rb_obj_dup(hash);
+ result = hash_alloc(rb_cHash);
+ hash_copy(result, hash);
for (i = 0; i < argc; i++) {
key = argv[i];
diff --git a/spec/ruby/core/hash/except_spec.rb b/spec/ruby/core/hash/except_spec.rb
index cef99b6284..82cfced72f 100644
--- a/spec/ruby/core/hash/except_spec.rb
+++ b/spec/ruby/core/hash/except_spec.rb
@@ -20,14 +20,15 @@ ruby_version_is "3.0" do
@hash.except(:a, :chunky_bacon).should == { b: 2, c: 3 }
end
- it "returns an instance of a subclass" do
+ it "always returns a Hash without a default" do
klass = Class.new(Hash)
- h = klass.new
+ h = klass.new(:default)
h[:bar] = 12
h[:foo] = 42
r = h.except(:foo)
r.should == {bar: 12}
- r.class.should == klass
+ r.class.should == Hash
+ r.default.should == nil
end
end
end