Class: Numeric

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
opal/opal/corelib/numeric.rb

Direct Known Subclasses

Float, Integer

Instance Method Summary collapse

Methods included from Comparable

#between?, normalize

Instance Method Details

#%(other) ⇒ Object Also known as: modulo



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'opal/opal/corelib/numeric.rb', line 86

def %(other)
  %x{
    if (other._isNumber) {
      if (other < 0 || self < 0) {
        return (self % other + other) % other;
      }
      else {
        return self % other;
      }
    }
    else {
      return #{send_coerced :%, other};
    }
  }
end

#&(other) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'opal/opal/corelib/numeric.rb', line 102

def &(other)
  %x{
    if (other._isNumber) {
      return self & other;
    }
    else {
      return #{send_coerced :&, other};
    }
  }
end

#*(other) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'opal/opal/corelib/numeric.rb', line 64

def *(other)
  %x{
    if (other._isNumber) {
      return self * other;
    }
    else {
      return #{send_coerced :*, other};
    }
  }
end

#**(other) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'opal/opal/corelib/numeric.rb', line 212

def **(other)
  %x{
    if (other._isNumber) {
      return Math.pow(self, other);
    }
    else {
      return #{send_coerced :**, other};
    }
  }
end

#+(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/numeric.rb', line 42

def +(other)
  %x{
    if (other._isNumber) {
      return self + other;
    }
    else {
      return #{send_coerced :+, other};
    }
  }
end

#+@Object



200
201
202
# File 'opal/opal/corelib/numeric.rb', line 200

def +@
  `+self`
end

#-(other) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'opal/opal/corelib/numeric.rb', line 53

def -(other)
  %x{
    if (other._isNumber) {
      return self - other;
    }
    else {
      return #{send_coerced :-, other};
    }
  }
end

#-@Object



204
205
206
# File 'opal/opal/corelib/numeric.rb', line 204

def -@
  `-self`
end

#/(other) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'opal/opal/corelib/numeric.rb', line 75

def /(other)
  %x{
    if (other._isNumber) {
      return self / other;
    }
    else {
      return #{send_coerced :/, other};
    }
  }
end

#<(other) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'opal/opal/corelib/numeric.rb', line 135

def <(other)
  %x{
    if (other._isNumber) {
      return self < other;
    }
    else {
      return #{send_coerced :<, other};
    }
  }
end

#<<(count) ⇒ Object



192
193
194
# File 'opal/opal/corelib/numeric.rb', line 192

def <<(count)
  `self << #{count.to_int}`
end

#<=(other) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'opal/opal/corelib/numeric.rb', line 146

def <=(other)
  %x{
    if (other._isNumber) {
      return self <= other;
    }
    else {
      return #{send_coerced :<=, other};
    }
  }
end

#<=>(other) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'opal/opal/corelib/numeric.rb', line 179

def <=>(other)
  %x{
    if (other._isNumber) {
      return self > other ? 1 : (self < other ? -1 : 0);
    }
    else {
      return #{send_coerced :<=>, other};
    }
  }
rescue ArgumentError
  nil
end

#==(other) ⇒ Object Also known as: eql?, equal?



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'opal/opal/corelib/numeric.rb', line 223

def ==(other)
  %x{
    if (other._isNumber) {
      return self == Number(other);
    }
    else if (#{other.respond_to? :==}) {
      return #{other == self};
    }
    else {
      return false;
    }
  }
end

#>(other) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'opal/opal/corelib/numeric.rb', line 157

def >(other)
  %x{
    if (other._isNumber) {
      return self > other;
    }
    else {
      return #{send_coerced :>, other};
    }
  }
end

#>=(other) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'opal/opal/corelib/numeric.rb', line 168

def >=(other)
  %x{
    if (other._isNumber) {
      return self >= other;
    }
    else {
      return #{send_coerced :>=, other};
    }
  }
end

#>>(count) ⇒ Object



196
197
198
# File 'opal/opal/corelib/numeric.rb', line 196

def >>(count)
  `self >> #{count.to_int}`
end

#^(other) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'opal/opal/corelib/numeric.rb', line 124

def ^(other)
  %x{
    if (other._isNumber) {
      return self ^ other;
    }
    else {
      return #{send_coerced :^, other};
    }
  }
end

#absObject Also known as: magnitude



237
238
239
# File 'opal/opal/corelib/numeric.rb', line 237

def abs
  `Math.abs(self)`
end

#ceilObject



241
242
243
# File 'opal/opal/corelib/numeric.rb', line 241

def ceil
  `Math.ceil(self)`
end

#chrObject



245
246
247
# File 'opal/opal/corelib/numeric.rb', line 245

def chr
  `String.fromCharCode(self)`
end

#coerce(other, type = :operation) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'opal/opal/corelib/numeric.rb', line 10

def coerce(other, type = :operation)
  %x{
    if (other._isNumber) {
      return [self, other];
    }
    else {
      return #{other.coerce(self)};
    }
  }
rescue
  case type
  when :operation
    raise TypeError, "#{other.class} can't be coerce into Numeric"

  when :comparison
    raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
  end
end

#conjObject Also known as: conjugate



249
250
251
# File 'opal/opal/corelib/numeric.rb', line 249

def conj
  self
end

#divmod(rhs) ⇒ Object



380
381
382
383
384
385
# File 'opal/opal/corelib/numeric.rb', line 380

def divmod(rhs)
  q = (self / rhs).floor
  r = self % rhs

  [q, r]
end

#downto(finish, &block) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'opal/opal/corelib/numeric.rb', line 255

def downto(finish, &block)
  return enum_for :downto, finish unless block

  %x{
    for (var i = self; i >= finish; i--) {
      if (block(i) === $breaker) {
        return $breaker.$v;
      }
    }
  }

  self
end

#even?Boolean

Returns:



272
273
274
# File 'opal/opal/corelib/numeric.rb', line 272

def even?
  `self % 2 === 0`
end

#finite?Boolean

Returns:



414
415
416
# File 'opal/opal/corelib/numeric.rb', line 414

def finite?
  `self == Infinity || self == -Infinity`
end

#floorObject



276
277
278
# File 'opal/opal/corelib/numeric.rb', line 276

def floor
  `Math.floor(self)`
end

#hashObject



280
281
282
# File 'opal/opal/corelib/numeric.rb', line 280

def hash
  `self.toString()`
end

#infinite?Boolean

Returns:



418
419
420
421
422
423
424
# File 'opal/opal/corelib/numeric.rb', line 418

def infinite?
  if `self == Infinity`
    `+1`
  elsif `self == -Infinity`
    `-1`
  end
end

#integer?Boolean

Returns:



284
285
286
# File 'opal/opal/corelib/numeric.rb', line 284

def integer?
  `self % 1 === 0`
end

#is_a?(klass) ⇒ Boolean

Returns:



288
289
290
291
292
293
# File 'opal/opal/corelib/numeric.rb', line 288

def is_a?(klass)
  return true if klass == Float && Float === self
  return true if klass == Integer && Integer === self

  super
end

#nan?Boolean

Returns:



410
411
412
# File 'opal/opal/corelib/numeric.rb', line 410

def nan?
  `isNaN(self)`
end

#nextObject Also known as: succ



299
300
301
# File 'opal/opal/corelib/numeric.rb', line 299

def next
  `self + 1`
end

#nonzero?Boolean

Returns:



303
304
305
# File 'opal/opal/corelib/numeric.rb', line 303

def nonzero?
  `self == 0 ? nil : self`
end

#odd?Boolean

Returns:



307
308
309
# File 'opal/opal/corelib/numeric.rb', line 307

def odd?
  `self % 2 !== 0`
end

#ordObject



311
312
313
# File 'opal/opal/corelib/numeric.rb', line 311

def ord
  self
end

#predObject



315
316
317
# File 'opal/opal/corelib/numeric.rb', line 315

def pred
  `self - 1`
end

#send_coerced(method, other) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'opal/opal/corelib/numeric.rb', line 29

def send_coerced(method, other)
  type = case method
    when :+, :-, :*, :/, :%, :&, :|, :^, :**
      :operation

    when :>, :>=, :<, :<=, :<=>
      :comparison
  end

  a, b = coerce(other, type)
  a.__send__ method, b
end

#sizeObject



405
406
407
408
# File 'opal/opal/corelib/numeric.rb', line 405

def size
  # Just a stub, JS is 32bit for bitwise ops though
  4
end

#step(limit, step = 1, &block) ⇒ Object

Raises:



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'opal/opal/corelib/numeric.rb', line 319

def step(limit, step = 1, &block)
  return enum_for :step, limit, step unless block

  raise ArgumentError, 'step cannot be 0' if `step == 0`

  %x{
    var value = self;

    if (step > 0) {
      while (value <= limit) {
        block(value);
        value += step;
      }
    }
    else {
      while (value >= limit) {
        block(value);
        value += step;
      }
    }
  }

  self
end

#times(&block) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'opal/opal/corelib/numeric.rb', line 346

def times(&block)
  return enum_for :times unless block

  %x{
    for (var i = 0; i < self; i++) {
      if (block(i) === $breaker) {
        return $breaker.$v;
      }
    }
  }

  self
end

#to_fObject



360
361
362
# File 'opal/opal/corelib/numeric.rb', line 360

def to_f
  `parseFloat(#{self})`
end

#to_iObject Also known as: to_int



364
365
366
# File 'opal/opal/corelib/numeric.rb', line 364

def to_i
  `parseInt(#{self})`
end

#to_s(base = 10) ⇒ Object Also known as: inspect



370
371
372
373
374
375
376
# File 'opal/opal/corelib/numeric.rb', line 370

def to_s(base = 10)
  if base < 2 || base > 36
    raise ArgumentError, 'base must be between 2 and 36'
  end

  `self.toString(base)`
end

#upto(finish, &block) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'opal/opal/corelib/numeric.rb', line 387

def upto(finish, &block)
  return enum_for :upto, finish unless block

  %x{
    for (var i = self; i <= finish; i++) {
      if (block(i) === $breaker) {
        return $breaker.$v;
      }
    }
  }

  self
end

#zero?Boolean

Returns:



401
402
403
# File 'opal/opal/corelib/numeric.rb', line 401

def zero?
  `self == 0`
end

#|(other) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'opal/opal/corelib/numeric.rb', line 113

def |(other)
  %x{
    if (other._isNumber) {
      return self | other;
    }
    else {
      return #{send_coerced :|, other};
    }
  }
end

#~Object



208
209
210
# File 'opal/opal/corelib/numeric.rb', line 208

def ~
  `~self`
end