Skip to content

Commit

Permalink
Fixed issue with Bignum hash values.
Browse files Browse the repository at this point in the history
  • Loading branch information
sporkmonger committed Dec 2, 2008
1 parent 3a63f8a commit 3b1e1ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/uuidtools.rb
Expand Up @@ -381,13 +381,15 @@ def to_s

# Returns an integer representation for this UUID.
def to_i
bytes = (time_low << 96) + (time_mid << 80) +
(time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
(clock_seq_low << 48)
for i in 0..5
bytes += (nodes[i] << (40 - (i * 8)))
end
return bytes
@integer ||= (begin
bytes = (time_low << 96) + (time_mid << 80) +
(time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
(clock_seq_low << 48)
for i in 0..5
bytes += (nodes[i] << (40 - (i * 8)))
end
bytes
end)
end

# Returns a URI string for this UUID.
Expand All @@ -397,7 +399,7 @@ def to_uri

# Returns an integer hash value.
def hash
return self.to_i
@hash ||= self.to_i % 0x3fffffff
end

# Returns true if this UUID is exactly equal to the other UUID.
Expand Down
4 changes: 3 additions & 1 deletion spec/uuidtools/uuid_parsing_spec.rb
Expand Up @@ -90,7 +90,9 @@
end

it "should produce a sane hash value for a UUID" do
UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).hash.should == 0
uuid = UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
uuid.to_i.should == 0
uuid.hash.should be_kind_of(Fixnum)
end

it "should produce the correct URI for a UUID" do
Expand Down

0 comments on commit 3b1e1ba

Please sign in to comment.