Skip to content

Instantly share code, notes, and snippets.

View flickgradley's full-sized avatar

Nick Bradley flickgradley

View GitHub Profile
@flickgradley
flickgradley / data_diff.rb
Created August 6, 2023 03:58
#diff method for Ruby Data classes
Measure = Data.define(:amount, :unit) do
def diff(other)
raise ArgumentError.new("other must be same class") unless other.is_a?(self.class)
members.inject({}) do |memo, key|
thisVal, otherVal = send(key), other.send(key)
memo[key] = [thisVal, otherVal] unless thisVal == otherVal
memo
end