Browse files

Merge pull request #2170 from brainopia/support_private_hash

Add and fix specs for Hash keys with private hash method
  • Loading branch information...
2 parents a787d83 + d20feaa commit abc1bc58d70de5d95d5ddf39b136b93d2cf86454 @dbussink dbussink committed Feb 24, 2013
View
7 kernel/common/hash18.rb
@@ -67,7 +67,7 @@ def []=(key, value)
redistribute @entries if @size > @max_entries
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
index = key_hash & @mask
item = @entries[index]
@@ -116,8 +116,7 @@ def default_proc
def delete(key)
Rubinius.check_frozen
-
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
ents = @entries
@@ -201,7 +200,7 @@ def fetch(key, default=undefined)
# Searches for an item matching +key+. Returns the item
# if found. Otherwise returns +nil+.
def find_item(key)
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
item = @entries[key_index(key_hash)]
while item
View
7 kernel/common/hash19.rb
@@ -122,7 +122,7 @@ def []=(key, value)
redistribute @entries if @size > @max_entries
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
index = key_hash & @mask
item = @entries[index]
@@ -198,8 +198,7 @@ def default_proc=(prc)
def delete(key)
Rubinius.check_frozen
-
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
index = key_index key_hash
if item = @entries[index]
@@ -262,7 +261,7 @@ def fetch(key, default=undefined)
# Searches for an item matching +key+. Returns the item
# if found. Otherwise returns +nil+.
def find_item(key)
- key_hash = key.hash
+ key_hash = Rubinius.privately { key.hash }
item = @entries[key_index(key_hash)]
while item
View
5 spec/ruby/core/hash/delete_spec.rb
@@ -20,6 +20,11 @@
new_hash { :defualt }.delete(:d).should == nil
end
+ it "accepts keys with private #hash method" do
+ key = HashSpecs::KeyWithPrivateHash.new
+ new_hash(key => 5).delete(key).should == 5
+ end
+
ruby_version_is "" ... "1.9" do
it "raises a TypeError if called on a frozen instance" do
lambda { HashSpecs.frozen_hash.delete("foo") }.should raise_error(TypeError)
View
5 spec/ruby/core/hash/element_reference_spec.rb
@@ -112,4 +112,9 @@
new_hash(x => :x)[x].should == :x
end
+
+ it "supports keys with private #hash method" do
+ key = HashSpecs::KeyWithPrivateHash.new
+ new_hash(key => 42)[key].should == 42
+ end
end
View
4 spec/ruby/core/hash/fixtures/classes.rb
@@ -29,6 +29,10 @@ def to_hash
end
end
+ class KeyWithPrivateHash
+ private :hash
+ end
+
def self.empty_frozen_hash
@empty ||= new_hash
@empty.freeze
View
7 spec/ruby/core/hash/shared/store.rb
@@ -27,6 +27,13 @@ def key.reverse() "bar" end
h.size.should == 2
end
+ it "accepts keys with private #hash method" do
+ key = HashSpecs::KeyWithPrivateHash.new
+ h = new_hash
+ h[key] = "foo"
+ h[key].should == "foo"
+ end
+
it "duplicates and freezes string keys" do
key = "foo"
h = new_hash

0 comments on commit abc1bc5

Please sign in to comment.