Class: Code::Object::Decimal
Constant Summary
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary
#methods, #raw
Instance Method Summary
collapse
code_new, #code_new, maybe, #name, repeat, |
#<=>, #==, #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_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #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) ⇒ 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, &_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)
else
args.first.to_s.to_d
end
else
0.to_d
end
rescue FloatDomainError
self.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
|
# 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 "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 "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
|
137
138
139
|
# File 'lib/code/object/decimal.rb', line 137
def code_abs
Decimal.new(raw.abs)
end
|
329
330
331
|
# File 'lib/code/object/decimal.rb', line 329
def code_any?
Boolean.new(raw > 0)
end
|
#code_bitwise_and(other) ⇒ Object
141
142
143
144
145
|
# File 'lib/code/object/decimal.rb', line 141
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
147
148
149
150
151
|
# File 'lib/code/object/decimal.rb', line 147
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
153
154
155
156
157
|
# File 'lib/code/object/decimal.rb', line 153
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
159
160
161
162
163
164
|
# File 'lib/code/object/decimal.rb', line 159
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_compare(other) ⇒ Object
166
167
168
169
170
|
# File 'lib/code/object/decimal.rb', line 166
def code_compare(other)
code_other = other.to_code
Integer.new(raw <=> code_other.raw)
end
|
#code_division(other) ⇒ Object
172
173
174
175
176
|
# File 'lib/code/object/decimal.rb', line 172
def code_division(other)
code_other = other.to_code
Decimal.new(raw / code_other.raw)
end
|
#code_eight? ⇒ Boolean
178
179
180
|
# File 'lib/code/object/decimal.rb', line 178
def code_eight?
Boolean.new(raw == 8)
end
|
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
190
191
|
# File 'lib/code/object/decimal.rb', line 186
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
|
193
194
195
|
# File 'lib/code/object/decimal.rb', line 193
def code_four?
Boolean.new(raw == 4)
end
|
#code_inferior(other) ⇒ Object
197
198
199
200
201
|
# File 'lib/code/object/decimal.rb', line 197
def code_inferior(other)
code_other = other.to_code
Boolean.new(raw < code_other.raw)
end
|
#code_inferior_or_equal(other) ⇒ Object
203
204
205
206
207
|
# File 'lib/code/object/decimal.rb', line 203
def code_inferior_or_equal(other)
code_other = other.to_code
Boolean.new(raw <= code_other.raw)
end
|
#code_left_shift(other) ⇒ Object
209
210
211
212
213
|
# File 'lib/code/object/decimal.rb', line 209
def code_left_shift(other)
code_other = other.to_code
Integer.new(raw.to_i << code_other.raw.to_i)
end
|
325
326
327
|
# File 'lib/code/object/decimal.rb', line 325
def code_many?
Boolean.new(raw > 1)
end
|
#code_minus(other) ⇒ Object
215
216
217
218
219
|
# File 'lib/code/object/decimal.rb', line 215
def code_minus(other)
code_other = other.to_code
Decimal.new(raw - code_other.raw)
end
|
#code_modulo(other) ⇒ Object
221
222
223
224
225
|
# File 'lib/code/object/decimal.rb', line 221
def code_modulo(other)
code_other = other.to_code
Decimal.new(raw % code_other.raw)
end
|
#code_multiplication(other) ⇒ Object
227
228
229
230
231
|
# File 'lib/code/object/decimal.rb', line 227
def code_multiplication(other)
code_other = other.to_code
Decimal.new(raw * code_other.raw)
end
|
233
234
235
|
# File 'lib/code/object/decimal.rb', line 233
def code_nine?
Boolean.new(raw == 9)
end
|
237
238
239
|
# File 'lib/code/object/decimal.rb', line 237
def code_one?
Boolean.new(raw == 1)
end
|
#code_plus(other) ⇒ Object
241
242
243
244
245
246
247
248
249
|
# File 'lib/code/object/decimal.rb', line 241
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
251
252
253
254
255
|
# File 'lib/code/object/decimal.rb', line 251
def code_power(other)
code_other = other.to_code
Decimal.new(raw**code_other.raw)
end
|
#code_right_shift(other) ⇒ Object
257
258
259
260
261
|
# File 'lib/code/object/decimal.rb', line 257
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
263
264
265
266
267
268
|
# File 'lib/code/object/decimal.rb', line 263
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
270
271
272
|
# File 'lib/code/object/decimal.rb', line 270
def code_seven?
Boolean.new(raw == 7)
end
|
274
275
276
|
# File 'lib/code/object/decimal.rb', line 274
def code_six?
Boolean.new(raw == 6)
end
|
#code_sqrt ⇒ Object
278
279
280
|
# File 'lib/code/object/decimal.rb', line 278
def code_sqrt
Decimal.new(Math.sqrt(raw).to_s)
end
|
#code_superior(other) ⇒ Object
282
283
284
285
286
|
# File 'lib/code/object/decimal.rb', line 282
def code_superior(other)
code_other = other.to_code
Boolean.new(raw > code_other.raw)
end
|
#code_superior_or_equal(other) ⇒ Object
288
289
290
291
292
|
# File 'lib/code/object/decimal.rb', line 288
def code_superior_or_equal(other)
code_other = other.to_code
Boolean.new(raw >= code_other.raw)
end
|
294
295
296
|
# File 'lib/code/object/decimal.rb', line 294
def code_ten?
Boolean.new(raw == 10)
end
|
#code_three? ⇒ Boolean
298
299
300
|
# File 'lib/code/object/decimal.rb', line 298
def code_three?
Boolean.new(raw == 3)
end
|
#code_to_string ⇒ Object
302
303
304
|
# File 'lib/code/object/decimal.rb', line 302
def code_to_string
String.new(raw.to_s("F"))
end
|
#code_truncate(n = nil) ⇒ Object
306
307
308
309
310
311
|
# File 'lib/code/object/decimal.rb', line 306
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
|
313
314
315
|
# File 'lib/code/object/decimal.rb', line 313
def code_two?
Boolean.new(raw == 2)
end
|
#code_unary_minus ⇒ Object
317
318
319
|
# File 'lib/code/object/decimal.rb', line 317
def code_unary_minus
Decimal.new(-raw)
end
|
321
322
323
|
# File 'lib/code/object/decimal.rb', line 321
def code_zero?
Boolean.new(raw.zero?)
end
|