Created
July 24, 2013 16:31
-
-
Save evanphx/6072176 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 Minutes | |
def initialize(count) | |
@count = count | |
end | |
def to_i | |
@count | |
end | |
def +(o) | |
Minutes.new(@count + o.to_i) | |
end | |
def -(o) | |
Minutes.new(@count - o.to_i) | |
end | |
def *(o) | |
Minutes.new(@count * o.to_i) | |
end | |
def /(o) | |
Minutes.new(@count / o.to_i) | |
end | |
def coerce(x) | |
[Minutes.new(@count), Minutes.new(x.to_i)] | |
end | |
attr_reader :count | |
end | |
m = Minutes.new(5) | |
p(m + 8) | |
p(m - 2) | |
p(m * 2) | |
p(m / 2) | |
Oh yeah, should be self there.
Didn't know if I was missing something that would blow my mind. BTW, I've wanted to thank you for a while. Thanks for getting my friend an interpreter for RubyConf last year. He had the best time.
No problem! Glad it worked out. Do you know if he's planning on coming to RubyConf this year?
I have no idea. I'll ask him right now.
K. hit me back up on twitter when you find out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it normal to call the new again on the lhs in coerce? I usually put self there.