class Mesopotamian
Mesopotamian
numbers
Public Class Methods
Create a new mesopotamian numeral @param [Numeric,Array] num
# File lib/mesopotamian.rb, line 11 def initialize(num) case num when Numeric @i = num.to_i when Array sum = 0 num.reverse.each_with_index do |n,i| sum += n * 60**i end @i = sum else raise ArgumentError.new "Cannot convert #{num}" end end
Public Instance Methods
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 153 def &(o) Mesopotamian.new(@i & o.to_int) end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 128 def *(o) op(:*, o) end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 138 def **(o) op(:**, o) end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 118 def +(o) op(:+, o) end
Unary Plus—Returns the receiver's value. @return [Mesopotamian]
# File lib/mesopotamian.rb, line 107 def +@ self end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 123 def -(o) op(:-, o) end
Unary Minus—Returns the receiver's value, negated. @return [Mesopotamian]
# File lib/mesopotamian.rb, line 113 def -@ Mesopotamian.new(-@i) end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 133 def /(o) op(:/, o) end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 143 def <<(o) Mesopotamian.new(@i << o.to_int) end
Returns zero if this equals other
, otherwise nil
is returned if the two values are incomparable. @return [Numeric]
# File lib/mesopotamian.rb, line 165 def <=>(o) case o when Mesopotamian @i <=> o.to_i when Numeric @i <=> o else a, b = o.coerce(self) a <=> b end rescue nil end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 148 def >>(o) Mesopotamian.new(@i >> o.to_int) end
Returns false
if a
is less than zero or if b
is greater than zero, true otherwise. @return [Boolean]
# File lib/mesopotamian.rb, line 89 def between?(a, b) @i.between? a, b end
If this is the same type as o
, returns an array containing this and num
. @return [Array<Mesopotamian>]
# File lib/mesopotamian.rb, line 101 def coerce(o) [Mesopotamian.new(o.to_int), self] end
Returns true
if num
and this are the same type and have equal values. @return [Boolean]
# File lib/mesopotamian.rb, line 54 def eql?(num) self.class.equal?(num.class) && @i == num.to_i end
Returns true
if this is an even number. @return [Boolean]
# File lib/mesopotamian.rb, line 83 def even? @i.even? end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 178 def freeze to_m super end
@return [Numeric]
# File lib/mesopotamian.rb, line 61 def hash @i.to_hash end
Since a Mesopotamian
is an Integer
, this always returns true
. @return [Boolean]
# File lib/mesopotamian.rb, line 95 def integer? true end
@return [Boolean]
# File lib/mesopotamian.rb, line 71 def nonzero? @i.nonzero? end
Returns true
if this is an odd number. @return [Boolean]
# File lib/mesopotamian.rb, line 77 def odd? @i.odd? end
# File lib/mesopotamian.rb, line 40 def to_a MesopotamianMath::Conversions.sexa_value @i end
Returns an integer representation of this Mesopotamian
in base 10. @return [Integer]
# File lib/mesopotamian.rb, line 34 def to_i @i end
As this is already a Mesopotamian
, simply return itself. @return [Mesopotamian]
# File lib/mesopotamian.rb, line 46 def to_m self end
Return a string representation of this Mesopotamian
. @return [String]
# File lib/mesopotamian.rb, line 28 def to_s @s ||= build_string.freeze end
@return [Boolean]
# File lib/mesopotamian.rb, line 66 def zero? @i.zero? end
@return [Mesopotamian]
# File lib/mesopotamian.rb, line 158 def |(o) Mesopotamian.new(@i | o.to_int) end
Private Instance Methods
# File lib/mesopotamian.rb, line 185 def build_string sexa = MesopotamianMath::Conversions.sexa_value @i if (sexa.is_a?(Integer) || sexa.is_a?(Array)) return MesopotamianMath::Conversions.sexa_to_string sexa else # We shouldn't be able to end up here raise TypeError.new "Something is wrong with the value #{sexa}, it's neither Integer nor Array" end "" end
# File lib/mesopotamian.rb, line 196 def op(sym, o) case o when Mesopotamian Mesopotamian.new(@i.send(sym, o.to_i)) when Numeric Mesopotamian.new(@i.send(sym, o)) else a, b = o.coerce(self) a.send(sym, b) end end