Class: Mesopotamian

Inherits:
Numeric
  • Object
show all
Includes:
Comparable
Defined in:
lib/mesopotamian.rb

Overview

Mesopotamian numbers

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ Mesopotamian

Create a new mesopotamian numeral

Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#&(o) ⇒ Mesopotamian

Returns:



153
154
155
# File 'lib/mesopotamian.rb', line 153

def &(o)
  Mesopotamian.new(@i & o.to_int)
end

#*(o) ⇒ Mesopotamian

Returns:



128
129
130
# File 'lib/mesopotamian.rb', line 128

def *(o)
  op(:*, o)
end

#**(o) ⇒ Mesopotamian

Returns:



138
139
140
# File 'lib/mesopotamian.rb', line 138

def **(o)
  op(:**, o)
end

#+(o) ⇒ Mesopotamian

Returns:



118
119
120
# File 'lib/mesopotamian.rb', line 118

def +(o)
  op(:+, o)
end

#+@Mesopotamian

Unary Plus—Returns the receiver’s value.

Returns:



107
108
109
# File 'lib/mesopotamian.rb', line 107

def +@
  self
end

#-(o) ⇒ Mesopotamian

Returns:



123
124
125
# File 'lib/mesopotamian.rb', line 123

def -(o)
  op(:-, o)
end

#-@Mesopotamian

Unary Minus—Returns the receiver’s value, negated.

Returns:



113
114
115
# File 'lib/mesopotamian.rb', line 113

def -@
  Mesopotamian.new(-@i)
end

#/(o) ⇒ Mesopotamian

Returns:



133
134
135
# File 'lib/mesopotamian.rb', line 133

def /(o)
  op(:/, o)
end

#<<(o) ⇒ Mesopotamian

Returns:



143
144
145
# File 'lib/mesopotamian.rb', line 143

def <<(o)
  Mesopotamian.new(@i << o.to_int)
end

#<=>(o) ⇒ Numeric

Returns zero if this equals other, otherwise nil is returned if the two values are incomparable.

Returns:

  • (Numeric)


165
166
167
168
169
170
171
172
173
174
175
# 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

#>>(o) ⇒ Mesopotamian

Returns:



148
149
150
# File 'lib/mesopotamian.rb', line 148

def >>(o)
  Mesopotamian.new(@i >> o.to_int)
end

#between?(a, b) ⇒ Boolean

Returns false if a is less than zero or if b is greater than zero, true otherwise.

Returns:

  • (Boolean)


89
90
91
# File 'lib/mesopotamian.rb', line 89

def between?(a, b)
  @i.between? a, b
end

#coerce(o) ⇒ Array<Mesopotamian>

If this is the same type as o, returns an array containing this and num.

Returns:



101
102
103
# File 'lib/mesopotamian.rb', line 101

def coerce(o)
  [Mesopotamian.new(o.to_int), self]
end

#eql?(num) ⇒ Boolean Also known as: ==

Returns true if num and this are the same type and have equal values.

Returns:

  • (Boolean)


54
55
56
# File 'lib/mesopotamian.rb', line 54

def eql?(num)
  self.class.equal?(num.class) && @i == num.to_i
end

#even?Boolean

Returns true if this is an even number.

Returns:

  • (Boolean)


83
84
85
# File 'lib/mesopotamian.rb', line 83

def even?
  @i.even?
end

#freezeMesopotamian

Returns:



178
179
180
181
# File 'lib/mesopotamian.rb', line 178

def freeze
  to_m
  super
end

#hashNumeric

Returns:

  • (Numeric)


61
62
63
# File 'lib/mesopotamian.rb', line 61

def hash
  @i.to_hash
end

#integer?Boolean

Since a Mesopotamian is an Integer, this always returns true.

Returns:

  • (Boolean)


95
96
97
# File 'lib/mesopotamian.rb', line 95

def integer?
  true
end

#nonzero?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mesopotamian.rb', line 71

def nonzero?
  @i.nonzero?
end

#odd?Boolean

Returns true if this is an odd number.

Returns:

  • (Boolean)


77
78
79
# File 'lib/mesopotamian.rb', line 77

def odd?
  @i.odd?
end

#to_aObject



40
41
42
# File 'lib/mesopotamian.rb', line 40

def to_a
  MesopotamianMath::Conversions.sexa_value @i
end

#to_iInteger Also known as: to_int

Returns an integer representation of this Mesopotamian in base 10.

Returns:



34
35
36
# File 'lib/mesopotamian.rb', line 34

def to_i
  @i
end

#to_mMesopotamian Also known as: to_sex

As this is already a Mesopotamian, simply return itself.

Returns:



46
47
48
# File 'lib/mesopotamian.rb', line 46

def to_m
  self
end

#to_sString

Return a string representation of this Mesopotamian.

Returns:

  • (String)


28
29
30
# File 'lib/mesopotamian.rb', line 28

def to_s
  @s ||= build_string.freeze
end

#zero?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mesopotamian.rb', line 66

def zero?
  @i.zero?
end

#|(o) ⇒ Mesopotamian

Returns:



158
159
160
# File 'lib/mesopotamian.rb', line 158

def |(o)
  Mesopotamian.new(@i | o.to_int)
end