Class: Mesopotamian
- Inherits:
-
Numeric
- Object
- Numeric
- Mesopotamian
- Includes:
- Comparable
- Defined in:
- lib/mesopotamian.rb
Overview
Mesopotamian numbers
Instance Method Summary collapse
- #&(o) ⇒ Mesopotamian
- #*(o) ⇒ Mesopotamian
- #**(o) ⇒ Mesopotamian
- #+(o) ⇒ Mesopotamian
-
#+@ ⇒ Mesopotamian
Unary Plus—Returns the receiver’s value.
- #-(o) ⇒ Mesopotamian
-
#-@ ⇒ Mesopotamian
Unary Minus—Returns the receiver’s value, negated.
- #/(o) ⇒ Mesopotamian
- #<<(o) ⇒ Mesopotamian
-
#<=>(o) ⇒ Numeric
Returns zero if this equals
other
, otherwisenil
is returned if the two values are incomparable. - #>>(o) ⇒ Mesopotamian
-
#between?(a, b) ⇒ Boolean
Returns
false
ifa
is less than zero or ifb
is greater than zero, true otherwise. -
#coerce(o) ⇒ Array<Mesopotamian>
If this is the same type as
o
, returns an array containing this andnum
. -
#eql?(num) ⇒ Boolean
(also: #==)
Returns
true
ifnum
and this are the same type and have equal values. -
#even? ⇒ Boolean
Returns
true
if this is an even number. - #freeze ⇒ Mesopotamian
- #hash ⇒ Numeric
-
#initialize(num) ⇒ Mesopotamian
constructor
Create a new mesopotamian numeral.
-
#integer? ⇒ Boolean
Since a Mesopotamian is an Integer, this always returns
true
. - #nonzero? ⇒ Boolean
-
#odd? ⇒ Boolean
Returns
true
if this is an odd number. - #to_a ⇒ Object
-
#to_i ⇒ Integer
(also: #to_int)
Returns an integer representation of this Mesopotamian in base 10.
-
#to_m ⇒ Mesopotamian
(also: #to_sex)
As this is already a Mesopotamian, simply return itself.
-
#to_s ⇒ String
Return a string representation of this Mesopotamian.
- #zero? ⇒ Boolean
- #|(o) ⇒ Mesopotamian
Constructor Details
#initialize(num) ⇒ Mesopotamian
Create a new mesopotamian numeral
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
153 154 155 |
# File 'lib/mesopotamian.rb', line 153 def &(o) Mesopotamian.new(@i & o.to_int) end |
#+@ ⇒ Mesopotamian
Unary Plus—Returns the receiver’s value.
107 108 109 |
# File 'lib/mesopotamian.rb', line 107 def +@ self end |
#-@ ⇒ Mesopotamian
Unary Minus—Returns the receiver’s value, negated.
113 114 115 |
# File 'lib/mesopotamian.rb', line 113 def -@ Mesopotamian.new(-@i) end |
#<<(o) ⇒ Mesopotamian
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.
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
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.
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
.
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.
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.
83 84 85 |
# File 'lib/mesopotamian.rb', line 83 def even? @i.even? end |
#freeze ⇒ Mesopotamian
178 179 180 181 |
# File 'lib/mesopotamian.rb', line 178 def freeze to_m super end |
#hash ⇒ 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
.
95 96 97 |
# File 'lib/mesopotamian.rb', line 95 def integer? true end |
#nonzero? ⇒ 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.
77 78 79 |
# File 'lib/mesopotamian.rb', line 77 def odd? @i.odd? end |
#to_a ⇒ Object
40 41 42 |
# File 'lib/mesopotamian.rb', line 40 def to_a MesopotamianMath::Conversions.sexa_value @i end |
#to_i ⇒ Integer Also known as: to_int
Returns an integer representation of this Mesopotamian in base 10.
34 35 36 |
# File 'lib/mesopotamian.rb', line 34 def to_i @i end |
#to_m ⇒ Mesopotamian Also known as: to_sex
As this is already a Mesopotamian, simply return itself.
46 47 48 |
# File 'lib/mesopotamian.rb', line 46 def to_m self end |
#to_s ⇒ String
Return a string representation of this Mesopotamian.
28 29 30 |
# File 'lib/mesopotamian.rb', line 28 def to_s @s ||= build_string.freeze end |
#zero? ⇒ Boolean
66 67 68 |
# File 'lib/mesopotamian.rb', line 66 def zero? @i.zero? end |
#|(o) ⇒ Mesopotamian
158 159 160 |
# File 'lib/mesopotamian.rb', line 158 def |(o) Mesopotamian.new(@i | o.to_int) end |