Class: Code::Object::Decimal

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

Instance Attribute Summary collapse

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_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #falsy?, falsy?, hash, #hash, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(decimal, exponent: nil) ⇒ Decimal

Returns a new instance of Decimal.



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

def initialize(decimal, exponent: nil)
  @raw = BigDecimal(decimal)

  return unless exponent
  unless exponent.is_a?(Number)
    raise ::Code::Error::TypeError, "exponent is not a number"
  end

  @raw *= 10**exponent.raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/code/object/decimal.rb', line 6

def raw
  @raw
end

Class Method Details

.nameObject



19
20
21
# File 'lib/code/object/decimal.rb', line 19

def self.name
  "Decimal"
end

Instance Method Details

#call(**args) ⇒ Object



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
# File 'lib/code/object/decimal.rb', line 23

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

  case operator.to_s
  when "%", "modulo"
    sig(args) { Number }
    code_modulo(value)
  when "&", "bitwise_and"
    sig(args) { Number }
    code_bitwise_and(value)
  when "*", "multiplication"
    sig(args) { Number }
    code_multiplication(value)
  when "**", "power"
    sig(args) { Number }
    code_power(value)
  when "+", "plus"
    sig(args) { Object.maybe }
    value ? code_plus(value) : self
  when "-", "minus"
    sig(args) { Number.maybe }
    value ? code_minus(value) : code_unary_minus
  when "/", "division"
    sig(args) { Number }
    code_division(value)
  when "<", "inferior"
    sig(args) { Number }
    code_inferior(value)
  when "<<", "left_shift"
    sig(args) { Number }
    code_left_shift(value)
  when "<=", "inferior_or_equal"
    sig(args) { Number }
    code_inferior_or_equal(value)
  when "<=>", "compare"
    sig(args) { Number }
    code_compare(value)
  when ">", "superior"
    sig(args) { Number }
    code_superior(value)
  when ">=", "superior_or_equal"
    sig(args) { Number }
    code_superior_or_equal(value)
  when ">>", "right_shift"
    sig(args) { Number }
    code_right_shift(value)
  when "^", "bitwise_xor"
    sig(args) { Number }
    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 "eight?"
    sig(args)
    code_eight?
  when "five?"
    sig(args)
    code_five?
  when "floor"
    sig(args) { Integer.maybe }
    code_floor(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(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(value)
  when "two?"
    sig(args)
    code_two?
  when "zero?"
    sig(args)
    code_zero?
  when "|", "bitwise_or"
    sig(args) { Number }
    code_bitwise_or(value)
  else
    super
  end
end

#code_absObject



145
146
147
# File 'lib/code/object/decimal.rb', line 145

def code_abs
  Decimal.new(raw.abs)
end

#code_bitwise_and(other) ⇒ Object



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

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

#code_bitwise_or(other) ⇒ Object



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

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

#code_bitwise_xor(other) ⇒ Object



157
158
159
# File 'lib/code/object/decimal.rb', line 157

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

#code_ceil(n = nil) ⇒ Object



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

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

#code_cloneObject



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

def code_clone
  Decimal.new(raw)
end

#code_compare(other) ⇒ Object



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

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

#code_division(other) ⇒ Object



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

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

#code_eight?Boolean

Returns:



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

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

#code_five?Boolean

Returns:



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

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

#code_floor(n = nil) ⇒ Object



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

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

#code_four?Boolean

Returns:



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

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

#code_inferior(other) ⇒ Object



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

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

#code_inferior_or_equal(other) ⇒ Object



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

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

#code_left_shift(other) ⇒ Object



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

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

#code_minus(other) ⇒ Object



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

def code_minus(other)
  Decimal.new(raw - other.raw)
end

#code_modulo(other) ⇒ Object



211
212
213
# File 'lib/code/object/decimal.rb', line 211

def code_modulo(other)
  Decimal.new(raw % other.raw)
end

#code_multiplication(other) ⇒ Object



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

def code_multiplication(other)
  Decimal.new(raw * other.raw)
end

#code_nine?Boolean

Returns:



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

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

#code_one?Boolean

Returns:



223
224
225
# File 'lib/code/object/decimal.rb', line 223

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

#code_plus(other) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/code/object/decimal.rb', line 227

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

#code_power(other) ⇒ Object



235
236
237
# File 'lib/code/object/decimal.rb', line 235

def code_power(other)
  Decimal.new(raw**other.raw)
end

#code_right_shift(other) ⇒ Object



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

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

#code_round(n = nil) ⇒ Object



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

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

#code_seven?Boolean

Returns:



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

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

#code_six?Boolean

Returns:



252
253
254
# File 'lib/code/object/decimal.rb', line 252

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

#code_sqrtObject



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

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

#code_superior(other) ⇒ Object



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

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

#code_superior_or_equal(other) ⇒ Object



264
265
266
# File 'lib/code/object/decimal.rb', line 264

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

#code_ten?Boolean

Returns:



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

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

#code_three?Boolean

Returns:



272
273
274
# File 'lib/code/object/decimal.rb', line 272

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

#code_to_decimalObject



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

def code_to_decimal
  Decimal.new(raw)
end

#code_to_integerObject



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

def code_to_integer
  Integer.new(raw.to_i)
end

#code_to_stringObject



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

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

#code_truncate(n = nil) ⇒ Object



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

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

#code_two?Boolean

Returns:



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

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

#code_unary_minusObject



297
298
299
# File 'lib/code/object/decimal.rb', line 297

def code_unary_minus
  Decimal.new(-raw)
end

#code_zero?Boolean

Returns:



301
302
303
# File 'lib/code/object/decimal.rb', line 301

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

#inspectObject



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

def inspect
  to_s
end

#to_sObject



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

def to_s
  raw.to_s("F")
end