Class: Code::Object::Decimal

Inherits:
Code::Object show all
Defined in:
lib/code/object/decimal.rb

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, code_different, #code_different, #code_equal_equal, code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ Decimal

Returns a new instance of Decimal.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/code/object/decimal.rb', line 6

def initialize(*args, **_kargs, &)
  @raw =
    if args.first.class.in?(NUMBER_CLASSES)
      if args.second.class.in?(NUMBER_CLASSES)
        args.first.to_s.to_d * (10**args.second.to_s.to_d)
      else
        args.first.to_s.to_d
      end
    else
      0.to_d
    end
rescue FloatDomainError
  @raw = 0.to_d
end

Instance Method Details

#call(**args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/code/object/decimal.rb', line 21

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "%", "modulo"
    sig(args) { Integer | Decimal }
    code_modulo(code_value)
  when "&", "bitwise_and"
    sig(args) { Integer | Decimal }
    code_bitwise_and(code_value)
  when "*", "multiplication"
    sig(args) { Integer | Decimal }
    code_multiplication(code_value)
  when "**", "power"
    sig(args) { Integer | Decimal }
    code_power(code_value)
  when "+", "plus"
    sig(args) { Object.maybe }
    code_arguments.any? ? code_plus(code_value) : self
  when "-", "minus"
    sig(args) { (Integer | Decimal).maybe }
    code_arguments.any? ? code_minus(code_value) : code_unary_minus
  when "/", "division"
    sig(args) { Integer | Decimal }
    code_division(code_value)
  when "<", "inferior"
    sig(args) { Integer | Decimal }
    code_inferior(code_value)
  when "<<", "left_shift"
    sig(args) { Integer | Decimal }
    code_left_shift(code_value)
  when "<=", "inferior_or_equal"
    sig(args) { Integer | Decimal }
    code_inferior_or_equal(code_value)
  when "<=>", "compare"
    sig(args) { Integer | Decimal }
    code_compare(code_value)
  when ">", "superior"
    sig(args) { Integer | Decimal }
    code_superior(code_value)
  when ">=", "superior_or_equal"
    sig(args) { Integer | Decimal }
    code_superior_or_equal(code_value)
  when ">>", "right_shift"
    sig(args) { Integer | Decimal }
    code_right_shift(code_value)
  when "^", "bitwise_xor"
    sig(args) { Integer | Decimal }
    code_bitwise_xor(code_value)
  when "abs"
    sig(args)
    code_abs
  when "ceil"
    sig(args) { Integer.maybe }
    code_ceil(code_value)
  when "clone"
    sig(args)
    code_clone
  when "eight?"
    sig(args)
    code_eight?
  when "five?"
    sig(args)
    code_five?
  when "floor"
    sig(args) { Integer.maybe }
    code_floor(code_value)
  when "four?"
    sig(args)
    code_four?
  when "nine?"
    sig(args)
    code_nine?
  when "one?"
    sig(args)
    code_one?
  when "round"
    sig(args) { Integer.maybe }
    code_round(code_value)
  when "seven?"
    sig(args)
    code_seven?
  when "six?"
    sig(args)
    code_six?
  when "sqrt"
    sig(args)
    code_sqrt
  when "ten?"
    sig(args)
    code_ten?
  when "three?"
    sig(args)
    code_three?
  when "to_decimal"
    sig(args)
    code_to_decimal
  when "to_integer"
    sig(args)
    code_to_integer
  when "to_string"
    sig(args)
    code_to_string
  when "truncate"
    sig(args) { Integer.maybe }
    code_truncate(code_value)
  when "two?"
    sig(args)
    code_two?
  when "zero?"
    sig(args)
    code_zero?
  when "|", "bitwise_or"
    sig(args) { Integer | Decimal }
    code_bitwise_or(code_value)
  else
    super
  end
end

#code_absObject



143
144
145
# File 'lib/code/object/decimal.rb', line 143

def code_abs
  Decimal.new(raw.abs)
end

#code_bitwise_and(other) ⇒ Object



147
148
149
150
151
# File 'lib/code/object/decimal.rb', line 147

def code_bitwise_and(other)
  code_other = other.to_code

  Integer.new(raw.to_i & code_other.raw.to_i)
end

#code_bitwise_or(other) ⇒ Object



153
154
155
156
157
# File 'lib/code/object/decimal.rb', line 153

def code_bitwise_or(other)
  code_other = other.to_code

  Integer.new(raw.to_i | code_other.raw.to_i)
end

#code_bitwise_xor(other) ⇒ Object



159
160
161
162
163
# File 'lib/code/object/decimal.rb', line 159

def code_bitwise_xor(other)
  code_other = other.to_code

  Integer.new(raw.to_i ^ code_other.raw.to_i)
end

#code_ceil(n = nil) ⇒ Object



165
166
167
168
169
170
# File 'lib/code/object/decimal.rb', line 165

def code_ceil(n = nil)
  code_n = n.to_code
  code_n = Integer.new(0) if code_n.nothing?

  Decimal.new(raw.ceil(code_n.raw))
end

#code_cloneObject



172
173
174
# File 'lib/code/object/decimal.rb', line 172

def code_clone
  Decimal.new(raw)
end

#code_compare(other) ⇒ Object



176
177
178
179
180
# File 'lib/code/object/decimal.rb', line 176

def code_compare(other)
  code_other = other.to_code

  Integer.new(raw <=> code_other.raw)
end

#code_division(other) ⇒ Object



182
183
184
185
186
# File 'lib/code/object/decimal.rb', line 182

def code_division(other)
  code_other = other.to_code

  Decimal.new(raw / code_other.raw)
end

#code_eight?Boolean

Returns:



188
189
190
# File 'lib/code/object/decimal.rb', line 188

def code_eight?
  Boolean.new(raw == 8)
end

#code_five?Boolean

Returns:



192
193
194
# File 'lib/code/object/decimal.rb', line 192

def code_five?
  Boolean.new(raw == 5)
end

#code_floor(n = nil) ⇒ Object



196
197
198
199
200
201
# File 'lib/code/object/decimal.rb', line 196

def code_floor(n = nil)
  code_n = n.to_code
  code_n = Integer.new(0) if code_n.nothing?

  Decimal.new(raw.floor(code_n.raw))
end

#code_four?Boolean

Returns:



203
204
205
# File 'lib/code/object/decimal.rb', line 203

def code_four?
  Boolean.new(raw == 4)
end

#code_inferior(other) ⇒ Object



207
208
209
210
211
# File 'lib/code/object/decimal.rb', line 207

def code_inferior(other)
  code_other = other.to_code

  Boolean.new(raw < code_other.raw)
end

#code_inferior_or_equal(other) ⇒ Object



213
214
215
216
217
# File 'lib/code/object/decimal.rb', line 213

def code_inferior_or_equal(other)
  code_other = other.to_code

  Boolean.new(raw <= code_other.raw)
end

#code_left_shift(other) ⇒ Object



219
220
221
222
223
# File 'lib/code/object/decimal.rb', line 219

def code_left_shift(other)
  code_other = other.to_code

  Integer.new(raw.to_i << code_other.raw.to_i)
end

#code_minus(other) ⇒ Object



225
226
227
228
229
# File 'lib/code/object/decimal.rb', line 225

def code_minus(other)
  code_other = other.to_code

  Decimal.new(raw - code_other.raw)
end

#code_modulo(other) ⇒ Object



231
232
233
234
235
# File 'lib/code/object/decimal.rb', line 231

def code_modulo(other)
  code_other = other.to_code

  Decimal.new(raw % code_other.raw)
end

#code_multiplication(other) ⇒ Object



237
238
239
240
241
# File 'lib/code/object/decimal.rb', line 237

def code_multiplication(other)
  code_other = other.to_code

  Decimal.new(raw * code_other.raw)
end

#code_nine?Boolean

Returns:



243
244
245
# File 'lib/code/object/decimal.rb', line 243

def code_nine?
  Boolean.new(raw == 9)
end

#code_one?Boolean

Returns:



247
248
249
# File 'lib/code/object/decimal.rb', line 247

def code_one?
  Boolean.new(raw == 1)
end

#code_plus(other) ⇒ Object



251
252
253
254
255
256
257
258
259
# File 'lib/code/object/decimal.rb', line 251

def code_plus(other)
  code_other = other.to_code

  if code_other.is_an?(Integer) || other.is_a?(Decimal)
    Decimal.new(raw + code_other.raw)
  else
    String.new(to_s + code_other.to_s)
  end
end

#code_power(other) ⇒ Object



261
262
263
264
265
# File 'lib/code/object/decimal.rb', line 261

def code_power(other)
  code_other = other.to_code

  Decimal.new(raw**code_other.raw)
end

#code_right_shift(other) ⇒ Object



267
268
269
270
271
# File 'lib/code/object/decimal.rb', line 267

def code_right_shift(other)
  code_other = other.to_code

  Integer.new(raw.to_i >> code_other.raw.to_i)
end

#code_round(n = nil) ⇒ Object



273
274
275
276
277
278
# File 'lib/code/object/decimal.rb', line 273

def code_round(n = nil)
  code_n = n.to_code
  code_n = Integer.new(0) if code_n.nothing?

  Decimal.new(raw.round(code_n.raw))
end

#code_seven?Boolean

Returns:



280
281
282
# File 'lib/code/object/decimal.rb', line 280

def code_seven?
  Boolean.new(raw == 7)
end

#code_six?Boolean

Returns:



284
285
286
# File 'lib/code/object/decimal.rb', line 284

def code_six?
  Boolean.new(raw == 6)
end

#code_sqrtObject



288
289
290
# File 'lib/code/object/decimal.rb', line 288

def code_sqrt
  Decimal.new(Math.sqrt(raw).to_s)
end

#code_superior(other) ⇒ Object



292
293
294
295
296
# File 'lib/code/object/decimal.rb', line 292

def code_superior(other)
  code_other = other.to_code

  Boolean.new(raw > code_other.raw)
end

#code_superior_or_equal(other) ⇒ Object



298
299
300
301
302
# File 'lib/code/object/decimal.rb', line 298

def code_superior_or_equal(other)
  code_other = other.to_code

  Boolean.new(raw >= code_other.raw)
end

#code_ten?Boolean

Returns:



304
305
306
# File 'lib/code/object/decimal.rb', line 304

def code_ten?
  Boolean.new(raw == 10)
end

#code_three?Boolean

Returns:



308
309
310
# File 'lib/code/object/decimal.rb', line 308

def code_three?
  Boolean.new(raw == 3)
end

#code_to_decimalObject



312
313
314
# File 'lib/code/object/decimal.rb', line 312

def code_to_decimal
  Decimal.new(raw)
end

#code_to_integerObject



316
317
318
# File 'lib/code/object/decimal.rb', line 316

def code_to_integer
  Integer.new(raw.to_i)
end

#code_to_stringObject



320
321
322
# File 'lib/code/object/decimal.rb', line 320

def code_to_string
  String.new(raw.to_s("F"))
end

#code_truncate(n = nil) ⇒ Object



324
325
326
327
328
329
# File 'lib/code/object/decimal.rb', line 324

def code_truncate(n = nil)
  code_n = n.to_code
  code_n = Integer.new(0) if code_n.nothing?

  Decimal.new(raw.truncate(code_n.raw))
end

#code_two?Boolean

Returns:



331
332
333
# File 'lib/code/object/decimal.rb', line 331

def code_two?
  Boolean.new(raw == 2)
end

#code_unary_minusObject



335
336
337
# File 'lib/code/object/decimal.rb', line 335

def code_unary_minus
  Decimal.new(-raw)
end

#code_zero?Boolean

Returns:



339
340
341
# File 'lib/code/object/decimal.rb', line 339

def code_zero?
  Boolean.new(raw.zero?)
end