class Mesopotamian

Mesopotamian numbers

Public Class Methods

new(num) click to toggle source

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

&(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 153
def &(o)
  Mesopotamian.new(@i & o.to_int)
end
*(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 128
def *(o)
  op(:*, o)
end
**(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 138
def **(o)
  op(:**, o)
end
+(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 118
def +(o)
  op(:+, o)
end
+@() click to toggle source

Unary Plus—Returns the receiver's value. @return [Mesopotamian]

# File lib/mesopotamian.rb, line 107
def +@
  self
end
-(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 123
def -(o)
  op(:-, o)
end
-@() click to toggle source

Unary Minus—Returns the receiver's value, negated. @return [Mesopotamian]

# File lib/mesopotamian.rb, line 113
def -@
  Mesopotamian.new(-@i)
end
/(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 133
def /(o)
  op(:/, o)
end
<<(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 143
def <<(o)
  Mesopotamian.new(@i << o.to_int)
end
<=>(o) click to toggle source

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
==(num)
Alias for: eql?
>>(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 148
def >>(o)
  Mesopotamian.new(@i >> o.to_int)
end
between?(a, b) click to toggle source

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
coerce(o) click to toggle source

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
eql?(num) click to toggle source

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
Also aliased as: ==
even?() click to toggle source

Returns true if this is an even number. @return [Boolean]

# File lib/mesopotamian.rb, line 83
def even?
  @i.even?
end
freeze() click to toggle source

@return [Mesopotamian]

Calls superclass method
# File lib/mesopotamian.rb, line 178
def freeze
  to_m
  super
end
hash() click to toggle source

@return [Numeric]

# File lib/mesopotamian.rb, line 61
def hash
  @i.to_hash
end
integer?() click to toggle source

Since a Mesopotamian is an Integer, this always returns true. @return [Boolean]

# File lib/mesopotamian.rb, line 95
def integer?
  true
end
nonzero?() click to toggle source

@return [Boolean]

# File lib/mesopotamian.rb, line 71
def nonzero?
  @i.nonzero?
end
odd?() click to toggle source

Returns true if this is an odd number. @return [Boolean]

# File lib/mesopotamian.rb, line 77
def odd?
  @i.odd?
end
to_a() click to toggle source
# File lib/mesopotamian.rb, line 40
def to_a
  MesopotamianMath::Conversions.sexa_value @i
end
to_i() click to toggle source

Returns an integer representation of this Mesopotamian in base 10. @return [Integer]

# File lib/mesopotamian.rb, line 34
def to_i
  @i
end
Also aliased as: to_int
to_int()
Alias for: to_i
to_m() click to toggle source

As this is already a Mesopotamian, simply return itself. @return [Mesopotamian]

# File lib/mesopotamian.rb, line 46
def to_m
  self
end
Also aliased as: to_sex
to_s() click to toggle source

Return a string representation of this Mesopotamian. @return [String]

# File lib/mesopotamian.rb, line 28
def to_s
  @s ||= build_string.freeze
end
to_sex()
Alias for: to_m
zero?() click to toggle source

@return [Boolean]

# File lib/mesopotamian.rb, line 66
def zero?
  @i.zero?
end
|(o) click to toggle source

@return [Mesopotamian]

# File lib/mesopotamian.rb, line 158
def |(o)
  Mesopotamian.new(@i | o.to_int)
end

Private Instance Methods

build_string() click to toggle source
# 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
op(sym, o) click to toggle source
# 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