Skip to content

Instantly share code, notes, and snippets.

@louismrose
Last active October 15, 2015 13:52
Show Gist options
  • Save louismrose/4988b91c7f48bbab2b64 to your computer and use it in GitHub Desktop.
Save louismrose/4988b91c7f48bbab2b64 to your computer and use it in GitHub Desktop.
class MoneyUnique
attr_accessor :pounds, :pence
def initialize(pounds, pence)
@pounds = pounds
@pence = pence
end
end
four_quid = MoneyUnique.new(4, 0)
three_quid = MoneyUnique.new(3, 0)
four_quid_again = MoneyUnique.new(4, 0)
puts (four_quid == three_quid) # => false, good!
puts (four_quid == four_quid_again) # what does this print?
class Money
attr_accessor :pounds, :pence
def initialize(pounds, pence)
@pounds = pounds
@pence = pence
end
def ==(other)
other.pounds == pounds && other.pence == pence
end
alias_method :eql?, :==
end
# Now let's try the same code again, but using the class that provides its own
# equality logic
four_quid = Money.new(4, 0)
three_quid = Money.new(3, 0)
four_quid_again = Money.new(4, 0)
puts (four_quid == three_quid) # => false, good!
puts (four_quid == four_quid_again) # what does this print?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment