Class: Code::Object::Integer

Inherits:
Code::Object show all
Defined in:
lib/code/object/integer.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) ⇒ Integer

Returns a new instance of Integer.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/code/object/integer.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)).to_i
      else
        args.first.to_s.to_i
      end
    else
      0
    end
rescue FloatDomainError
  @raw = 0
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/code/object/integer.rb', line 21

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  globals = multi_fetch(args, *GLOBALS)
  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 | String }
    code_multiplication(code_value)
  when "**", "power"
    sig(args) { Integer | Decimal }
    code_power(code_value)
  when "+", "plus", "self"
    sig(args) { Object.maybe }
    code_arguments.any? ? code_plus(code_value) : code_self
  when "-", "minus", "unary_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 "day", "days"
    sig(args)
    code_days
  when "decrement!"
    sig(args) { Integer.maybe }
    code_decrement!(code_value)
  when "decrement"
    sig(args) { Integer.maybe }
    code_decrement(code_value)
  when "eight?"
    sig(args)
    code_eight?
  when "even?"
    sig(args)
    code_even?
  when "five?"
    sig(args)
    code_five?
  when "floor"
    sig(args) { Integer.maybe }
    code_floor(code_value)
  when "four?"
    sig(args)
    code_four?
  when "hour", "hours"
    sig(args)
    code_hours
  when "increment!"
    sig(args) { Integer.maybe }
    code_increment!(code_value)
  when "increment"
    sig(args) { Integer.maybe }
    code_increment(code_value)
  when "nine?"
    sig(args)
    code_nine?
  when "odd?"
    sig(args)
    code_odd?
  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 "times"
    sig(args) { Function }
    code_times(code_value, **globals)
  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



162
163
164
# File 'lib/code/object/integer.rb', line 162

def code_abs
  Decimal.new(raw.abs)
end

#code_bitwise_and(other) ⇒ Object



166
167
168
169
# File 'lib/code/object/integer.rb', line 166

def code_bitwise_and(other)
  code_other = other.to_code
  Integer.new(raw & code_other.raw.to_i)
end

#code_bitwise_or(other) ⇒ Object



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

def code_bitwise_or(other)
  code_other = other.to_code
  Integer.new(raw | code_other.raw.to_i)
end

#code_bitwise_xor(other) ⇒ Object



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

def code_bitwise_xor(other)
  code_other = other.to_code
  Integer.new(raw ^ code_other.raw.to_i)
end

#code_ceil(n = nil) ⇒ Object



181
182
183
184
185
# File 'lib/code/object/integer.rb', line 181

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

#code_cloneObject



187
188
189
# File 'lib/code/object/integer.rb', line 187

def code_clone
  Integer.new(raw)
end

#code_compare(other) ⇒ Object



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

def code_compare(other)
  code_other = other.to_code
  Integer.new(raw <=> code_other.raw)
end

#code_daysObject



418
419
420
# File 'lib/code/object/integer.rb', line 418

def code_days
  Duration.new(raw.days)
end

#code_decrement(n = nil) ⇒ Object



203
204
205
206
207
# File 'lib/code/object/integer.rb', line 203

def code_decrement(n = nil)
  code_n = n.to_code
  code_n = Integer.new(1) if code_n.nothing?
  Integer.new(raw - code_n.raw)
end

#code_decrement!(n = nil) ⇒ Object



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

def code_decrement!(n = nil)
  code_n = n.to_code
  code_n = Integer.new(1) if code_n.nothing?
  @raw -= code_n.raw
  self
end

#code_division(other) ⇒ Object



209
210
211
212
# File 'lib/code/object/integer.rb', line 209

def code_division(other)
  code_other = other.to_code
  Decimal.new(BigDecimal(raw) / code_other.raw)
end

#code_eight?Boolean

Returns:



214
215
216
# File 'lib/code/object/integer.rb', line 214

def code_eight?
  Boolean.new(raw.eight?)
end

#code_even?Boolean

Returns:



218
219
220
# File 'lib/code/object/integer.rb', line 218

def code_even?
  Boolean.new(raw.even?)
end

#code_five?Boolean

Returns:



222
223
224
# File 'lib/code/object/integer.rb', line 222

def code_five?
  Boolean.new(raw.five?)
end

#code_floor(n = nil) ⇒ Object



226
227
228
229
230
# File 'lib/code/object/integer.rb', line 226

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

#code_four?Boolean

Returns:



232
233
234
# File 'lib/code/object/integer.rb', line 232

def code_four?
  Boolean.new(raw.four?)
end

#code_hoursObject



414
415
416
# File 'lib/code/object/integer.rb', line 414

def code_hours
  Duration.new(raw.hours)
end

#code_increment(n = nil) ⇒ Object



243
244
245
246
247
# File 'lib/code/object/integer.rb', line 243

def code_increment(n = nil)
  code_n = n.to_code
  code_n = Integer.new(1) if code_n.nothing?
  Integer.new(raw + code_n.raw)
end

#code_increment!(n = nil) ⇒ Object



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

def code_increment!(n = nil)
  code_n = n.to_code
  code_n = Integer.new(1) if code_n.nothing?
  @raw += code_n.raw
  self
end

#code_inferior(other) ⇒ Object



249
250
251
252
# File 'lib/code/object/integer.rb', line 249

def code_inferior(other)
  code_other = other.to_code
  Boolean.new(raw < code_other.raw)
end

#code_inferior_or_equal(other) ⇒ Object



254
255
256
257
# File 'lib/code/object/integer.rb', line 254

def code_inferior_or_equal(other)
  code_other = other.to_code
  Boolean.new(raw <= code_other.raw)
end

#code_left_shift(other) ⇒ Object



259
260
261
262
# File 'lib/code/object/integer.rb', line 259

def code_left_shift(other)
  code_other = other.to_code
  Integer.new(raw << code_other.raw.to_i)
end

#code_minus(other) ⇒ Object



264
265
266
267
268
269
270
271
272
# File 'lib/code/object/integer.rb', line 264

def code_minus(other)
  code_other = other.to_code

  if code_other.is_a?(Integer)
    Integer.new(raw - code_other.raw)
  else
    Decimal.new(raw - code_other.raw)
  end
end

#code_modulo(other) ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'lib/code/object/integer.rb', line 274

def code_modulo(other)
  code_other = other.to_code

  if code_other.is_a?(Integer)
    Integer.new(raw % code_other.raw)
  else
    Decimal.new(raw % code_other.raw)
  end
end

#code_multiplication(other) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/code/object/integer.rb', line 284

def code_multiplication(other)
  code_other = other.to_code

  if code_other.is_a?(Integer)
    Integer.new(raw * code_other.raw)
  elsif code_other.is_a?(Decimal)
    Decimal.new(raw * code_other.raw)
  else
    String.new(code_other.raw * raw)
  end
end

#code_nine?Boolean

Returns:



296
297
298
# File 'lib/code/object/integer.rb', line 296

def code_nine?
  Boolean.new(raw.nine?)
end

#code_odd?Boolean

Returns:



300
301
302
# File 'lib/code/object/integer.rb', line 300

def code_odd?
  Boolean.new(raw.odd?)
end

#code_one?Boolean

Returns:



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

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

#code_plus(other) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/code/object/integer.rb', line 308

def code_plus(other)
  code_other = other.to_code

  if code_other.is_a?(Integer)
    Integer.new(raw + code_other.raw)
  elsif code_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



320
321
322
323
324
325
326
327
328
# File 'lib/code/object/integer.rb', line 320

def code_power(other)
  code_other = other.to_code

  if code_other.is_a?(Integer)
    Integer.new(raw**other.raw)
  else
    Decimal.new(raw**other.raw)
  end
end

#code_right_shift(other) ⇒ Object



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

def code_right_shift(other)
  code_other = other.to_code
  Integer.new(raw >> code_other.raw.to_i)
end

#code_round(n = nil) ⇒ Object



335
336
337
338
339
340
# File 'lib/code/object/integer.rb', line 335

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

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

#code_seven?Boolean

Returns:



342
343
344
# File 'lib/code/object/integer.rb', line 342

def code_seven?
  Boolean.new(raw.seven?)
end

#code_six?Boolean

Returns:



346
347
348
# File 'lib/code/object/integer.rb', line 346

def code_six?
  Boolean.new(raw.six?)
end

#code_sqrtObject



350
351
352
# File 'lib/code/object/integer.rb', line 350

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

#code_superior(other) ⇒ Object



354
355
356
357
358
# File 'lib/code/object/integer.rb', line 354

def code_superior(other)
  code_other = other.to_code

  Boolean.new(raw > code_other.raw)
end

#code_superior_or_equal(other) ⇒ Object



360
361
362
363
364
# File 'lib/code/object/integer.rb', line 360

def code_superior_or_equal(other)
  code_other = other.to_code

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

#code_ten?Boolean

Returns:



366
367
368
# File 'lib/code/object/integer.rb', line 366

def code_ten?
  Boolean.new(raw.ten?)
end

#code_three?Boolean

Returns:



370
371
372
# File 'lib/code/object/integer.rb', line 370

def code_three?
  Boolean.new(raw.three?)
end

#code_times(argument, **globals) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/code/object/integer.rb', line 382

def code_times(argument, **globals)
  code_argument = argument.to_code

  raw.times do |element|
    code_argument.call(
      arguments: List.new([Integer.new(element), self]),
      **globals
    )
  end

  self
end

#code_to_decimalObject



374
375
376
# File 'lib/code/object/integer.rb', line 374

def code_to_decimal
  Decimal.new(raw)
end

#code_to_integerObject



378
379
380
# File 'lib/code/object/integer.rb', line 378

def code_to_integer
  Integer.new(raw)
end

#code_truncate(n = nil) ⇒ Object



395
396
397
398
399
400
# File 'lib/code/object/integer.rb', line 395

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

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

#code_two?Boolean

Returns:



402
403
404
# File 'lib/code/object/integer.rb', line 402

def code_two?
  Boolean.new(raw.two?)
end

#code_unary_minusObject



406
407
408
# File 'lib/code/object/integer.rb', line 406

def code_unary_minus
  Integer.new(-raw)
end

#code_zero?Boolean

Returns:



410
411
412
# File 'lib/code/object/integer.rb', line 410

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