Created
February 19, 2011 03:23
-
-
Save gom/834793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module MyTime | |
class TimeBase | |
BASE = 60 | |
def initialize i | |
@i = i | |
end | |
def to_i | |
@i | |
end | |
def to_s | |
@i.to_s | |
end | |
# TODO: add math operator | |
def method_missing method, *args | |
if @i.respond_to? method | |
r = @i.__send__(method, *args) | |
if r.kind_of? Fixnum | |
@i = r | |
else | |
@i = r if r.respond_to? :to_i | |
end | |
self | |
else | |
raise NameError | |
end | |
end | |
end | |
class Hour < TimeBase | |
def to_minutes | |
(@i * BASE).to_minutes | |
end | |
def to_seconds | |
to_minutes.to_seconds | |
end | |
end | |
class Minute < TimeBase | |
def to_hours | |
(@i / BASE).to_hours | |
end | |
def to_seconds | |
(@i * BASE).to_seconds | |
end | |
end | |
class Second < TimeBase | |
def to_hours | |
(@i / (BASE^2)).to_hours | |
end | |
def to_minutes | |
(@i / BASE).to_minutes | |
end | |
end | |
def hours | |
Hour.new self | |
end | |
def hour | |
hours | |
end | |
def minutes | |
Minute.new self | |
end | |
def seconds | |
Second.new self | |
end | |
def to_hours | |
hours | |
end | |
def to_minutes | |
minutes | |
end | |
def to_seconds | |
seconds | |
end | |
end | |
class Numeric | |
include MyTime | |
def m | |
(self.k).k | |
end | |
def k | |
self * 1000 | |
end | |
def b | |
((self.k).k).k | |
end | |
def h | |
self * 100 | |
end | |
end | |
class Fixnum | |
%w(+ - * / += -= *= /= ** %).each do |op| | |
define_method(op) {|n| eval "self.to_f #{op} n" } | |
end | |
end | |
class Object | |
def dump | |
Marshal.dump(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment