class EmailAddress
include Comparable
def initialize(string)
if string =~ /@/
@raw_email_address = string.downcase.strip
else
raise ArgumentError, "email address must have an '@'"
end
end
def <=>(other)
raw_email_address <=> other
end
def to_s
raw_email_address
end
protected
attr_reader :raw_email_address
end
$ irb>> require "./email_address" #=> true
>> EmailAddress.new("user@example.com") == "user@example.com" #=> false (would have previously been true)
>> EmailAddress.new("user@example.com") == EmailAddress.new("user@example.com") #=> true
网友评论