Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created June 26, 2013 00:28
Show Gist options
  • Save garybernhardt/5863735 to your computer and use it in GitHub Desktop.
Save garybernhardt/5863735 to your computer and use it in GitHub Desktop.
--- /Users/grb/.rvm/gems/ruby-1.9.3-p194@investments/gems/values-1.5.0/lib/values.rb 2013-06-17 19:28:23.000000000 -0700
+++ values.rb 2013-06-25 17:28:17.000000000 -0700
@@ -1,7 +1,7 @@
class Value
def self.new(*fields)
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 @@
instance_variable_set(:"@#{field}", value)
end
+ @hash = values.map(&:hash).inject(0, :+) + self.class.hash
+
freeze
end
@@ -32,10 +34,6 @@
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment