Permalink
Browse files

Precompute hash with simpler function

  • Loading branch information...
1 parent 5f326a5 commit 99203f71778275139af8ff5512f0a147e1417c7a @garybernhardt garybernhardt committed Jun 26, 2013
Showing with 3 additions and 5 deletions.
  1. +3 −5 lib/values.rb
View
@@ -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

0 comments on commit 99203f7

Please sign in to comment.