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 included from Concerns::Shared

#methods, #raw

Instance Method Summary collapse

Methods inherited from Code::Object

code_new, #code_new, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #code_and, #code_as_json, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, #code_fetch, code_fetch, code_get, #code_get, #code_inclusive_range, #code_inspect, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_to_boolean, #code_to_class, #code_to_date, #code_to_dictionary, #code_to_duration, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ 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, &_block)
  self.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
  self.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
161
162
163
164
165
166
# 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)
  when "many?"
    sig(args)
    code_many?
  when "any?"
    sig(args)
    code_any?
  else
    super
  end
end

#code_absObject



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

def code_abs
  Decimal.new(raw.abs)
end

#code_any?Boolean

Returns:



424
425
426
# File 'lib/code/object/integer.rb', line 424

def code_any?
  Boolean.new(raw > 0)
end

#code_bitwise_and(other) ⇒ Object



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

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

#code_bitwise_or(other) ⇒ Object



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

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

#code_bitwise_xor(other) ⇒ Object



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

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

#code_ceil(n = nil) ⇒ Object



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

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



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

def code_clone
  Integer.new(raw)
end

#code_compare(other) ⇒ Object



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

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

#code_daysObject



432
433
434
# File 'lib/code/object/integer.rb', line 432

def code_days
  Duration.new(raw.days)
end

#code_decrement(n = nil) ⇒ Object



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

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



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

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



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

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

#code_eight?Boolean

Returns:



220
221
222
# File 'lib/code/object/integer.rb', line 220

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

#code_even?Boolean

Returns:



224
225
226
# File 'lib/code/object/integer.rb', line 224

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

#code_five?Boolean

Returns:



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

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

#code_floor(n = nil) ⇒ Object



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

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:



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

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

#code_hoursObject



428
429
430
# File 'lib/code/object/integer.rb', line 428

def code_hours
  Duration.new(raw.hours)
end

#code_increment(n = nil) ⇒ Object



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

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



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

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



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

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

#code_inferior_or_equal(other) ⇒ Object



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

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

#code_left_shift(other) ⇒ Object



265
266
267
268
# File 'lib/code/object/integer.rb', line 265

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

#code_many?Boolean

Returns:



420
421
422
# File 'lib/code/object/integer.rb', line 420

def code_many?
  Boolean.new(raw > 1)
end

#code_minus(other) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/code/object/integer.rb', line 270

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



280
281
282
283
284
285
286
287
288
# File 'lib/code/object/integer.rb', line 280

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



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/code/object/integer.rb', line 290

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:



302
303
304
# File 'lib/code/object/integer.rb', line 302

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

#code_odd?Boolean

Returns:



306
307
308
# File 'lib/code/object/integer.rb', line 306

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

#code_one?Boolean

Returns:



310
311
312
# File 'lib/code/object/integer.rb', line 310

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

#code_plus(other) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/code/object/integer.rb', line 314

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



326
327
328
329
330
331
332
333
334
# File 'lib/code/object/integer.rb', line 326

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



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

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

#code_round(n = nil) ⇒ Object



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

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:



348
349
350
# File 'lib/code/object/integer.rb', line 348

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

#code_six?Boolean

Returns:



352
353
354
# File 'lib/code/object/integer.rb', line 352

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

#code_sqrtObject



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

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

#code_superior(other) ⇒ Object



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

def code_superior(other)
  code_other = other.to_code

  Boolean.new(raw > code_other.raw)
end

#code_superior_or_equal(other) ⇒ Object



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

def code_superior_or_equal(other)
  code_other = other.to_code

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

#code_ten?Boolean

Returns:



372
373
374
# File 'lib/code/object/integer.rb', line 372

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

#code_three?Boolean

Returns:



376
377
378
# File 'lib/code/object/integer.rb', line 376

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

#code_times(argument, **globals) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/code/object/integer.rb', line 388

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



380
381
382
# File 'lib/code/object/integer.rb', line 380

def code_to_decimal
  Decimal.new(raw)
end

#code_to_integerObject



384
385
386
# File 'lib/code/object/integer.rb', line 384

def code_to_integer
  Integer.new(raw)
end

#code_truncate(n = nil) ⇒ Object



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

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:



408
409
410
# File 'lib/code/object/integer.rb', line 408

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

#code_unary_minusObject



412
413
414
# File 'lib/code/object/integer.rb', line 412

def code_unary_minus
  Integer.new(-raw)
end

#code_zero?Boolean

Returns:



416
417
418
# File 'lib/code/object/integer.rb', line 416

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