Last active
October 15, 2015 13:52
-
-
Save louismrose/4988b91c7f48bbab2b64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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