kaiser-ruby 0.7.1 → 0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,46 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:disable Style/RedundantSelf
1
3
  module KaiserRuby
4
+ # Rockstar introduces a new type that is similar to JS' undefined
5
+ # Ruby obviously doesn't have that so we have to make our own
6
+ class Mysterious
7
+ def to_bool
8
+ false
9
+ end
10
+
11
+ def +(other)
12
+ 'mysterious' + other if other.is_a?(String)
13
+ end
14
+
15
+ def -(other)
16
+ 'mysterious' + other if other.is_a?(String)
17
+ end
18
+
19
+ def to_s
20
+ 'mysterious'
21
+ end
22
+
23
+ def !
24
+ true
25
+ end
26
+
27
+ def ==(other)
28
+ return true if other.is_a?(KaiserRuby::Mysterious)
29
+
30
+ false
31
+ end
32
+
33
+ def !=(other)
34
+ !self.==(other)
35
+ end
36
+ end
37
+
38
+ # Breaking Ruby for fun and profit!
39
+ #
40
+ # This module is required to run the code that the transpiler generated, as Rockstar
41
+ # expects somewhat different behaviour of types than Ruby.
42
+ #
43
+ # Not a single method in here is a good idea and you should probably never use this code outside this gem.
2
44
  module Refinements
3
45
  refine NilClass do
4
46
  def to_bool
@@ -6,15 +48,53 @@ module KaiserRuby
6
48
  end
7
49
 
8
50
  def +(other)
9
- 'mysterious' + other if other.is_a?(String)
51
+ return 'null' + other if other.is_a?(String)
52
+
53
+ 0 + other
10
54
  end
11
55
 
12
56
  def -(other)
13
- 'mysterious' + other if other.is_a?(String)
57
+ return 'null' + other if other.is_a?(String)
58
+
59
+ 0 - other
60
+ end
61
+
62
+ def *(other)
63
+ 0 * other
64
+ end
65
+
66
+ def /(other)
67
+ 0 / other
68
+ end
69
+
70
+ def <(other)
71
+ 0 < other
72
+ end
73
+
74
+ def >(other)
75
+ 0 > other
76
+ end
77
+
78
+ def <=(other)
79
+ 0 <= other
80
+ end
81
+
82
+ def >=(other)
83
+ 0 >= other
84
+ end
85
+
86
+ def ==(other)
87
+ return false if other.is_a?(String) || other.is_a?(KaiserRuby::Mysterious) || other.is_a?(FalseClass) || other.is_a?(TrueClass)
88
+
89
+ 0 == other
90
+ end
91
+
92
+ def !=(other)
93
+ !self.==(other)
14
94
  end
15
95
 
16
96
  def to_s
17
- 'mysterious'
97
+ 'null'
18
98
  end
19
99
  end
20
100
 
@@ -27,38 +107,76 @@ module KaiserRuby
27
107
  alias_method :old_lt, :>
28
108
  alias_method :old_lte, :>=
29
109
  alias_method :old_eq, :==
110
+ alias_method :old_to_s, :to_s
30
111
 
31
112
  def to_bool
32
113
  self.zero? ? false : true
33
114
  end
34
115
 
35
116
  def +(other)
36
- other.is_a?(String) ? self.to_s + other : self.old_add(other)
117
+ if other.is_a?(String)
118
+ self.to_s + other
119
+ elsif other.is_a?(NilClass)
120
+ self + 0
121
+ else
122
+ self.old_add(other)
123
+ end
37
124
  end
38
125
 
39
126
  def *(other)
40
- other.is_a?(String) ? other * self : self.old_mul(other)
127
+ if other.is_a?(String)
128
+ other * self
129
+ elsif other.is_a?(NilClass)
130
+ self * 0
131
+ else
132
+ self.old_mul(other)
133
+ end
41
134
  end
42
135
 
43
136
  def /(other)
44
- raise ZeroDivisionError if other.zero?
137
+ raise ZeroDivisionError if other.zero? || other.is_a?(NilClass)
138
+
45
139
  self.old_div(other)
46
140
  end
47
141
 
48
142
  def <(other)
49
- other.is_a?(String) ? self < Float(other) : self.old_gt(other)
143
+ if other.is_a?(String)
144
+ self < Float(other)
145
+ elsif other.is_a?(NilClass)
146
+ self < 0
147
+ else
148
+ self.old_gt(other)
149
+ end
50
150
  end
51
151
 
52
152
  def <=(other)
53
- other.is_a?(String) ? self <= Float(other) : self.old_gte(other)
153
+ if other.is_a?(String)
154
+ self <= Float(other)
155
+ elsif other.is_a?(NilClass)
156
+ self <= 0
157
+ else
158
+ self.old_gte(other)
159
+ end
54
160
  end
55
161
 
56
162
  def >(other)
57
- other.is_a?(String) ? self > Float(other) : self.old_lt(other)
163
+ if other.is_a?(String)
164
+ self > Float(other)
165
+ elsif other.is_a?(NilClass)
166
+ self > 0
167
+ else
168
+ self.old_lt(other)
169
+ end
58
170
  end
59
171
 
60
172
  def >=(other)
61
- other.is_a?(String) ? self >= Float(other) : self.old_lte(other)
173
+ if other.is_a?(String)
174
+ self >= Float(other)
175
+ elsif other.is_a?(NilClass)
176
+ self >= 0
177
+ else
178
+ self.old_lte(other)
179
+ end
62
180
  end
63
181
 
64
182
  def ==(other)
@@ -67,10 +185,223 @@ module KaiserRuby
67
185
  elsif other.is_a?(String)
68
186
  t = Float(other) rescue other
69
187
  self.old_eq(t)
188
+ elsif other.is_a?(NilClass)
189
+ self.zero?
70
190
  else
71
191
  self.old_eq(other)
72
192
  end
73
193
  end
194
+
195
+ def !=(other)
196
+ !self.==(other)
197
+ end
198
+
199
+ def to_s
200
+ self.modulo(1).zero? ? self.to_i.to_s : self.old_to_s
201
+ end
202
+ end
203
+
204
+ # in Ruby 2.3 you have to refine Fixnum instead of Integer, which makes sense
205
+ # as merging Fixnum and BigInt into Integer was the 2.4 feature, so all is good
206
+ # BUT every Fixnum in 2.3 is_a?(Integer) so all the tests still work properly
207
+ if (RUBY_VERSION.split('.').map(&:to_i) <=> [2, 4]) >= 1
208
+ refine Integer do
209
+ alias_method :old_add, :+
210
+ alias_method :old_mul, :*
211
+ alias_method :old_div, :/
212
+ alias_method :old_gt, :<
213
+ alias_method :old_gte, :<=
214
+ alias_method :old_lt, :>
215
+ alias_method :old_lte, :>=
216
+ alias_method :old_eq, :==
217
+
218
+ def to_bool
219
+ self.zero? ? false : true
220
+ end
221
+
222
+ def +(other)
223
+ if other.is_a?(String)
224
+ self.to_s + other
225
+ elsif other.is_a?(NilClass)
226
+ self + 0
227
+ else
228
+ self.old_add(other)
229
+ end
230
+ end
231
+
232
+ def *(other)
233
+ if other.is_a?(String)
234
+ other * self
235
+ elsif other.is_a?(NilClass)
236
+ self * 0
237
+ else
238
+ self.old_mul(other)
239
+ end
240
+ end
241
+
242
+ def /(other)
243
+ raise ZeroDivisionError if other.zero? || other.is_a?(NilClass)
244
+
245
+ t = self.to_f.old_div(other)
246
+ t.modulo(1).zero? ? t.to_i : t
247
+ end
248
+
249
+ def <(other)
250
+ if other.is_a?(String)
251
+ self < Float(other)
252
+ elsif other.is_a?(NilClass)
253
+ self < 0
254
+ else
255
+ self.old_gt(other)
256
+ end
257
+ end
258
+
259
+ def <=(other)
260
+ if other.is_a?(String)
261
+ self <= Float(other)
262
+ elsif other.is_a?(NilClass)
263
+ self <= 0
264
+ else
265
+ self.old_gte(other)
266
+ end
267
+ end
268
+
269
+ def >(other)
270
+ if other.is_a?(String)
271
+ self > Float(other)
272
+ elsif other.is_a?(NilClass)
273
+ self > 0
274
+ else
275
+ self.old_lt(other)
276
+ end
277
+ end
278
+
279
+ def >=(other)
280
+ if other.is_a?(String)
281
+ self >= Float(other)
282
+ elsif other.is_a?(NilClass)
283
+ self >= 0
284
+ else
285
+ self.old_lte(other)
286
+ end
287
+ end
288
+
289
+ def ==(other)
290
+ if other.is_a?(TrueClass) || other.is_a?(FalseClass)
291
+ self.to_bool == other
292
+ elsif other.is_a?(String)
293
+ t = Float(other) rescue other
294
+ self.old_eq(t)
295
+ elsif other.is_a?(NilClass)
296
+ self.zero?
297
+ else
298
+ self.old_eq(other)
299
+ end
300
+ end
301
+
302
+ def !=(other)
303
+ !self.==(other)
304
+ end
305
+ end
306
+ else
307
+ refine Fixnum do
308
+ alias_method :old_add, :+
309
+ alias_method :old_mul, :*
310
+ alias_method :old_div, :/
311
+ alias_method :old_gt, :<
312
+ alias_method :old_gte, :<=
313
+ alias_method :old_lt, :>
314
+ alias_method :old_lte, :>=
315
+ alias_method :old_eq, :==
316
+
317
+ def to_bool
318
+ self.zero? ? false : true
319
+ end
320
+
321
+ def +(other)
322
+ if other.is_a?(String)
323
+ self.to_s + other
324
+ elsif other.is_a?(NilClass)
325
+ self + 0
326
+ else
327
+ self.old_add(other)
328
+ end
329
+ end
330
+
331
+ def *(other)
332
+ if other.is_a?(String)
333
+ other * self
334
+ elsif other.is_a?(NilClass)
335
+ self * 0
336
+ else
337
+ self.old_mul(other)
338
+ end
339
+ end
340
+
341
+ def /(other)
342
+ raise ZeroDivisionError if other.zero? || other.is_a?(NilClass)
343
+
344
+ t = self.to_f.old_div(other)
345
+ t.modulo(1).zero? ? t.to_i : t
346
+ end
347
+
348
+ def <(other)
349
+ if other.is_a?(String)
350
+ self < Float(other)
351
+ elsif other.is_a?(NilClass)
352
+ self < 0
353
+ else
354
+ self.old_gt(other)
355
+ end
356
+ end
357
+
358
+ def <=(other)
359
+ if other.is_a?(String)
360
+ self <= Float(other)
361
+ elsif other.is_a?(NilClass)
362
+ self <= 0
363
+ else
364
+ self.old_gte(other)
365
+ end
366
+ end
367
+
368
+ def >(other)
369
+ if other.is_a?(String)
370
+ self > Float(other)
371
+ elsif other.is_a?(NilClass)
372
+ self > 0
373
+ else
374
+ self.old_lt(other)
375
+ end
376
+ end
377
+
378
+ def >=(other)
379
+ if other.is_a?(String)
380
+ self >= Float(other)
381
+ elsif other.is_a?(NilClass)
382
+ self >= 0
383
+ else
384
+ self.old_lte(other)
385
+ end
386
+ end
387
+
388
+ def ==(other)
389
+ if other.is_a?(TrueClass) || other.is_a?(FalseClass)
390
+ self.to_bool == other
391
+ elsif other.is_a?(String)
392
+ t = Float(other) rescue other
393
+ self.old_eq(t)
394
+ elsif other.is_a?(NilClass)
395
+ self.zero?
396
+ else
397
+ self.old_eq(other)
398
+ end
399
+ end
400
+
401
+ def !=(other)
402
+ !self.==(other)
403
+ end
404
+ end
74
405
  end
75
406
 
76
407
  refine String do
@@ -80,39 +411,72 @@ module KaiserRuby
80
411
  alias_method :old_lt, :>
81
412
  alias_method :old_lte, :>=
82
413
  alias_method :old_eq, :==
414
+ alias_method :old_mul, :*
83
415
 
84
416
  def to_bool
85
- self.size == 0 ? false : true
417
+ self.size.zero? ? false : true
86
418
  end
87
419
 
88
420
  def __booleanize
89
- if self =~ /\A\bfalse\b|\bno\b|\blies\b|\bwrong\b\Z/i
90
- return false
91
- elsif self =~ /\A\btrue\b|\byes\b|\bok\b|\bright\b\Z/i
92
- return true
93
- end
421
+ # spec says these should be converted but tests says they should not. weird
422
+ # return false if self =~ /\A\bfalse\b|\bno\b|\blies\b|\bwrong\b\Z/i
423
+ # return true if self =~ /\A\btrue\b|\byes\b|\bok\b|\bright\b\Z/i
94
424
 
95
- return self
425
+ true
96
426
  end
97
427
 
98
428
  def +(other)
99
429
  other.is_a?(String) ? self.old_add(other) : self + other.to_s
100
430
  end
101
431
 
432
+ def *(other)
433
+ if other.is_a?(NilClass)
434
+ self.old_mul(0)
435
+ elsif other.is_a?(String)
436
+ KaiserRuby::Mysterious.new
437
+ else
438
+ self.old_mul(other)
439
+ end
440
+ end
441
+
102
442
  def <(other)
103
- other.is_a?(Float) ? Float(self) < other : self.old_gt(other)
443
+ if other.is_a?(Float)
444
+ Float(self) < other
445
+ elsif other.is_a?(Integer)
446
+ Integer(self) < other
447
+ else
448
+ self.old_gt(other)
449
+ end
104
450
  end
105
451
 
106
452
  def <=(other)
107
- other.is_a?(Float) ? Float(self) <= other : self.old_gte(other)
453
+ if other.is_a?(Float)
454
+ Float(self) <= other
455
+ elsif other.is_a?(Integer)
456
+ Integer(self) <= other
457
+ else
458
+ self.old_gte(other)
459
+ end
108
460
  end
109
461
 
110
462
  def >(other)
111
- other.is_a?(Float) ? Float(self) > other : self.old_lt(other)
463
+ if other.is_a?(Float)
464
+ Float(self) > other
465
+ elsif other.is_a?(Integer)
466
+ Integer(self) > other
467
+ else
468
+ self.old_lt(other)
469
+ end
112
470
  end
113
471
 
114
472
  def >=(other)
115
- other.is_a?(Float) ? Float(self) >= other : self.old_lte(other)
473
+ if other.is_a?(Float)
474
+ Float(self) >= other
475
+ elsif other.is_a?(Integer)
476
+ Integer(self) >= other
477
+ else
478
+ self.old_lte(other)
479
+ end
116
480
  end
117
481
 
118
482
  def ==(other)
@@ -121,10 +485,17 @@ module KaiserRuby
121
485
  elsif other.is_a?(Float)
122
486
  t = Float(self) rescue self
123
487
  t == other
488
+ elsif other.is_a?(Integer)
489
+ t = Integer(self) rescue self
490
+ t == other
124
491
  else
125
492
  self.old_eq(other)
126
493
  end
127
494
  end
495
+
496
+ def !=(other)
497
+ !self.==(other)
498
+ end
128
499
  end
129
500
 
130
501
  refine TrueClass do
@@ -136,6 +507,7 @@ module KaiserRuby
136
507
 
137
508
  def +(other)
138
509
  return 'true' + other if other.is_a?(String)
510
+
139
511
  other.even? ? self : !self
140
512
  end
141
513
 
@@ -144,7 +516,7 @@ module KaiserRuby
144
516
  end
145
517
 
146
518
  def ==(other)
147
- if other.is_a?(Float)
519
+ if other.is_a?(Float) || other.is_a?(Integer) || other.is_a?(NilClass)
148
520
  self == other.to_bool
149
521
  elsif other.is_a?(String)
150
522
  self.old_eq(other.__booleanize)
@@ -152,6 +524,10 @@ module KaiserRuby
152
524
  self.old_eq(other)
153
525
  end
154
526
  end
527
+
528
+ def !=(other)
529
+ !self.==(other)
530
+ end
155
531
  end
156
532
 
157
533
  refine FalseClass do
@@ -163,6 +539,7 @@ module KaiserRuby
163
539
 
164
540
  def +(other)
165
541
  return 'false' + other if other.is_a?(String)
542
+
166
543
  other.even? ? self : !self
167
544
  end
168
545
 
@@ -171,7 +548,7 @@ module KaiserRuby
171
548
  end
172
549
 
173
550
  def ==(other)
174
- if other.is_a?(Float)
551
+ if other.is_a?(Float) || other.is_a?(Integer) || other.is_a?(NilClass)
175
552
  self == other.to_bool
176
553
  elsif other.is_a?(String)
177
554
  self.old_eq(other.__booleanize)
@@ -179,6 +556,10 @@ module KaiserRuby
179
556
  self.old_eq(other)
180
557
  end
181
558
  end
559
+
560
+ def !=(other)
561
+ !self.==(other)
562
+ end
182
563
  end
183
564
 
184
565
  refine BasicObject do
@@ -186,15 +567,15 @@ module KaiserRuby
186
567
 
187
568
  def !
188
569
  if self.is_a?(String)
189
- return self.size == 0 ? true : false
190
- elsif self.is_a?(Float)
191
- return self.zero? ? true : false
570
+ self.size.zero?
571
+ elsif self.is_a?(Float) || self.is_a?(Integer)
572
+ self.zero?
192
573
  elsif self.is_a?(NilClass)
193
- return true
574
+ true
194
575
  else
195
576
  self.old_not
196
577
  end
197
578
  end
198
579
  end
199
580
  end
200
- end
581
+ end