Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Precompute hash with simpler function

  • Loading branch information...
commit 99203f71778275139af8ff5512f0a147e1417c7a 1 parent 5f326a5
@garybernhardt garybernhardt authored
Showing with 3 additions and 5 deletions.
  1. +3 −5 lib/values.rb
View
8 lib/values.rb
@@ -1,7 +1,7 @@
class Value
def self.new(*fields, &block)
Class.new do
- attr_reader(*fields)
+ attr_reader(:hash, *fields)
define_method(:initialize) do |*values|
raise ArgumentError.new("wrong number of arguments, #{values.size} for #{fields.size}") if fields.size != values.size
@@ -10,6 +10,8 @@ def self.new(*fields, &block)
instance_variable_set(:"@#{field}", value)
end
+ @hash = self.class.hash ^ values.hash
+
freeze
end
@@ -32,10 +34,6 @@ def eql?(other)
self.class == other.class && values == other.values
end
- def hash
- values.map(&:hash).inject(0, :+) + self.class.hash
- end
-
def values
self.class::VALUE_ATTRS.map { |field| send(field) }
end
Please sign in to comment.
Something went wrong with that request. Please try again.