Class: Code::Object::Integer

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

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #code_and_operator, code_and_operator, #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_string, code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ Integer

Returns a new instance of Integer.



6
7
8
9
10
11
12
13
14
# File 'lib/code/object/integer.rb', line 6

def initialize(*args, **_kargs, &_block)
  whole = args.first || 0
  exponent = args.second || 0
  whole = whole.raw if whole.is_an?(Object)
  exponent = exponent.raw if exponent.is_an?(Object)
  @raw = whole.to_i * 10**exponent
rescue FloatDomainError => e
  raise Error, "#{decimal.inspect} * 10**#{exponent.inspect} is invalid"
end

Class Method Details

.nameObject



16
17
18
# File 'lib/code/object/integer.rb', line 16

def self.name
  "Integer"
end

Instance Method Details

#as_jsonObject



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

def as_json(...)
  raw.as_json(...)
end

#call(**args) ⇒ Object



20
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
167
168
# File 'lib/code/object/integer.rb', line 20

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.first&.value

  case operator.to_s
  when "%", "modulo"
    sig(args) { Integer | Decimal }
    code_modulo(value)
  when "&", "bitwise_and"
    sig(args) { Integer | Decimal }
    code_bitwise_and(value)
  when "*", "multiplication", "×"
    sig(args) { Integer | Decimal | String }
    code_multiplication(value)
  when "**", "power"
    sig(args) { Integer | Decimal }
    code_power(value)
  when "+", "plus", "self"
    sig(args) { Object.maybe }
    value ? code_plus(value) : code_self
  when "-", "minus", "unary_minus"
    sig(args) { Integer | Decimal.maybe }
    value ? code_minus(value) : code_unary_minus
  when "/", "division", "÷"
    sig(args) { Integer | Decimal }
    code_division(value)
  when "<", "inferior"
    sig(args) { Integer | Decimal }
    code_inferior(value)
  when "<<", "left_shift"
    sig(args) { Integer | Decimal }
    code_left_shift(value)
  when "<=", "inferior_or_equal"
    sig(args) { Integer | Decimal }
    code_inferior_or_equal(value)
  when "<=>", "compare"
    sig(args) { Integer | Decimal }
    code_compare(value)
  when ">", "superior"
    sig(args) { Integer | Decimal }
    code_superior(value)
  when ">=", "superior_or_equal"
    sig(args) { Integer | Decimal }
    code_superior_or_equal(value)
  when ">>", "right_shift"
    sig(args) { Integer | Decimal }
    code_right_shift(value)
  when "^", "bitwise_xor"
    sig(args) { Integer | Decimal }
    code_bitwise_xor(value)
  when "abs"
    sig(args)
    code_abs
  when "ceil"
    sig(args) { Integer.maybe }
    code_ceil(value)
  when "clone"
    sig(args)
    code_clone
  when "day", "days"
    sig(args)
    code_days
  when "decrement!"
    sig(args) { Integer.maybe }
    code_decrement!(value)
  when "decrement"
    sig(args) { Integer.maybe }
    code_decrement(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(value)
  when "four?"
    sig(args)
    code_four?
  when "hour", "hours"
    sig(args)
    code_hours
  when "increment!"
    sig(args) { Integer.maybe }
    code_increment!(value)
  when "increment"
    sig(args) { Integer.maybe }
    code_increment(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(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(value, **globals)
  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(value)
  when "two?"
    sig(args)
    code_two?
  when "zero?"
    sig(args)
    code_zero?
  when "|", "bitwise_or"
    sig(args) { Integer | Decimal }
    code_bitwise_or(value)
  else
    super
  end
end

#code_absObject



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

def code_abs
  Decimal.new(raw.abs)
end

#code_bitwise_and(other) ⇒ Object



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

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

#code_bitwise_or(other) ⇒ Object



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

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

#code_bitwise_xor(other) ⇒ Object



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

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

#code_ceil(n = nil) ⇒ Object



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

def code_ceil(n = nil)
  n ||= Integer.new(0)
  Integer.new(raw.ceil(n.raw))
end

#code_cloneObject



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

def code_clone
  Integer.new(raw)
end

#code_compare(other) ⇒ Object



195
196
197
# File 'lib/code/object/integer.rb', line 195

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

#code_daysObject



391
392
393
# File 'lib/code/object/integer.rb', line 391

def code_days
  Duration.new(raw.days)
end

#code_decrement(n = nil) ⇒ Object



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

def code_decrement(n = nil)
  n ||= Integer.new(1)
  Integer.new(raw - n.raw)
end

#code_decrement!(n = nil) ⇒ Object



199
200
201
202
203
# File 'lib/code/object/integer.rb', line 199

def code_decrement!(n = nil)
  n ||= Integer.new(1)
  @raw -= n.raw
  self
end

#code_division(other) ⇒ Object



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

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

#code_eight?Boolean

Returns:



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

def code_eight?
  Boolean.new(raw == 8)
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 == 5)
end

#code_floor(n = nil) ⇒ Object



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

def code_floor(n = nil)
  n ||= Integer.new(0)
  Integer.new(raw.floor(n.raw))
end

#code_four?Boolean

Returns:



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

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

#code_hoursObject



387
388
389
# File 'lib/code/object/integer.rb', line 387

def code_hours
  Duration.new(raw.hours)
end

#code_increment(n = nil) ⇒ Object



241
242
243
244
# File 'lib/code/object/integer.rb', line 241

def code_increment(n = nil)
  n ||= Integer.new(1)
  Integer.new(raw + n.raw)
end

#code_increment!(n = nil) ⇒ Object



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

def code_increment!(n = nil)
  n ||= Integer.new(1)
  @raw += n.raw
  self
end

#code_inferior(other) ⇒ Object



246
247
248
# File 'lib/code/object/integer.rb', line 246

def code_inferior(other)
  Boolean.new(raw < other.raw)
end

#code_inferior_or_equal(other) ⇒ Object



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

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

#code_left_shift(other) ⇒ Object



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

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

#code_minus(other) ⇒ Object



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

def code_minus(other)
  if other.is_a?(Integer)
    Integer.new(raw - other.raw)
  else
    Decimal.new(raw - other.raw)
  end
end

#code_modulo(other) ⇒ Object



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

def code_modulo(other)
  if other.is_a?(Integer)
    Integer.new(raw % other.raw)
  else
    Decimal.new(raw % other.raw)
  end
end

#code_multiplication(other) ⇒ Object



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

def code_multiplication(other)
  if other.is_a?(Integer)
    Integer.new(raw * other.raw)
  elsif other.is_a?(Decimal)
    Decimal.new(raw * other.raw)
  else
    String.new(other.raw * raw)
  end
end

#code_nine?Boolean

Returns:



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

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

#code_odd?Boolean

Returns:



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

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

#code_one?Boolean

Returns:



292
293
294
# File 'lib/code/object/integer.rb', line 292

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

#code_plus(other) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/code/object/integer.rb', line 296

def code_plus(other)
  if other.is_a?(Integer)
    Integer.new(raw + other.raw)
  elsif other.is_a?(Decimal)
    Decimal.new(raw + other.raw)
  else
    String.new(to_s + other.to_s)
  end
end

#code_power(other) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/code/object/integer.rb', line 306

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

#code_right_shift(other) ⇒ Object



314
315
316
# File 'lib/code/object/integer.rb', line 314

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

#code_round(n = nil) ⇒ Object



318
319
320
321
# File 'lib/code/object/integer.rb', line 318

def code_round(n = nil)
  n ||= Integer.new(0)
  Integer.new(raw.round(n.raw))
end

#code_seven?Boolean

Returns:



323
324
325
# File 'lib/code/object/integer.rb', line 323

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

#code_six?Boolean

Returns:



327
328
329
# File 'lib/code/object/integer.rb', line 327

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

#code_sqrtObject



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

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

#code_superior(other) ⇒ Object



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

def code_superior(other)
  Boolean.new(raw > other.raw)
end

#code_superior_or_equal(other) ⇒ Object



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

def code_superior_or_equal(other)
  Boolean.new(raw >= other.raw)
end

#code_ten?Boolean

Returns:



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

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

#code_three?Boolean

Returns:



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

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

#code_times(argument, **globals) ⇒ Object



359
360
361
362
363
364
365
366
367
368
# File 'lib/code/object/integer.rb', line 359

def code_times(argument, **globals)
  raw.times do |element|
    argument.call(
      arguments: [Argument.new(Integer.new(element))],
      **globals
    )
  end

  self
end

#code_to_decimalObject



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

def code_to_decimal
  Decimal.new(raw)
end

#code_to_integerObject



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

def code_to_integer
  Integer.new(raw)
end

#code_truncate(n = nil) ⇒ Object



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

def code_truncate(n = nil)
  n ||= Integer.new(0)
  Integer.new(raw.truncate(n.raw))
end

#code_two?Boolean

Returns:



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

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

#code_unary_minusObject



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

def code_unary_minus
  Integer.new(-raw)
end

#code_zero?Boolean

Returns:



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

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

#inspectObject



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

def inspect
  to_s
end

#succObject



399
400
401
# File 'lib/code/object/integer.rb', line 399

def succ
  Integer.new(raw + 1)
end

#to_sObject



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

def to_s
  raw.to_s
end