Class: Numeric

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/source/ruby.rb

Overview

Numeric objects represent native JavaScript numbers. Class Numeric subsumes the methods of Ruby’s Float, Integer, Bignum, and Fixnum.

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #>, #>=, #between?

Instance Method Details

#%(n) ⇒ Object

call-seq:

num % other       -> numeric
num.modulo(other) -> numeric

Returns num modulo other. See Numeric#divmod for more information.



4112
4113
4114
# File 'lib/source/ruby.rb', line 4112

def %(n)
  `this%n`
end

#&(n) ⇒ Object

call-seq:

num & other -> integer

Performs bitwise AND between num and other. Floats are truncated to integers before operation.



4122
4123
4124
# File 'lib/source/ruby.rb', line 4122

def &(n)
  `this&n`
end

#*(n) ⇒ Object

call-seq:

num * other -> numeric

Multiplies num by other, then returns the result.



4172
4173
4174
# File 'lib/source/ruby.rb', line 4172

def *(n)
  `this*n`
end

#**(n) ⇒ Object

call-seq:

num ** n -> numeric

Raises num to the nth power, which may be negative or fractional.



4181
4182
4183
# File 'lib/source/ruby.rb', line 4181

def **(n)
  `Math.pow(this,n)`
end

#+(n) ⇒ Object

call-seq:

num + other -> numeric

Adds num to other, then returns the result.



4190
4191
4192
# File 'lib/source/ruby.rb', line 4190

def +(n)
  `this+n`
end

#+@Object

call-seq:

+num -> num

Unary Plus – returns the value of num.



4199
4200
4201
# File 'lib/source/ruby.rb', line 4199

def +@()
  `this`
end

#-(n) ⇒ Object

call-seq:

num - other -> numeric

Subtracts other from num, then returns the result.



4208
4209
4210
# File 'lib/source/ruby.rb', line 4208

def -(n)
  `this-n`
end

#-@Object

call-seq:

-num -> numeric

Unary Minus – returns the value of num negated.



4217
4218
4219
# File 'lib/source/ruby.rb', line 4217

def -@()
  `-this`
end

#/(n) ⇒ Object

call-seq:

num / other    -> integer
num.div(other) -> integer

Divides num by other, then truncates the result to an integer. (See also Numeric#quo.)



4163
4164
4165
# File 'lib/source/ruby.rb', line 4163

def /(n)
  `parseInt(this/n)`
end

#<<(n) ⇒ Object

call-seq:

num << n -> integer

Shifts num left n positions (right if n is negative). Floats are truncated to integers before operation.



4227
4228
4229
# File 'lib/source/ruby.rb', line 4227

def <<(n)
  `Math.floor(parseInt(this)*Math.pow(2,parseInt(n)))`
end

#<=>(n) ⇒ Object

call-seq:

num <=> numeric -> -1, 0, 1

Comparison – returns -1, 0, or 1 depending on whether num is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.



4248
4249
4250
4251
4252
4253
# File 'lib/source/ruby.rb', line 4248

def <=>(n)
  `if(n.constructor!=Number){return nil;}`
  `if(this>n){return 1;}`
  `if(this<n){return -1;}`
  return 0
end

#==(n) ⇒ Object

:nodoc:



4255
4256
4257
# File 'lib/source/ruby.rb', line 4255

def ==(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#===(n) ⇒ Object

:nodoc:



4259
4260
4261
# File 'lib/source/ruby.rb', line 4259

def ===(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#>>(n) ⇒ Object

call-seq:

num << n -> integer

Shifts num right n positions (left if n is negative). Floats are truncated to integers before operation.



4237
4238
4239
# File 'lib/source/ruby.rb', line 4237

def >>(n)
  `Math.floor(parseInt(this)/Math.pow(2,parseInt(n)))`
end

#[](n) ⇒ Object

call-seq:

num[n] -> 0, 1

Bit Reference – returns the nth bit in the binary representation of num, where num[0] is the least significant bit. Floats are truncated to integers before operation.

a = 1234
"%b" % a    #=> "10011010010"

15.downto(0) {|n| puts a[n] }

produces:

0000010011010010


4279
4280
4281
4282
4283
# File 'lib/source/ruby.rb', line 4279

def [](n)
  `var str=parseInt(this).toString(2)`
  `if(n>=str.length){return 0;}`
  `parseInt(str[(str.length-n-1)])`
end

#^(n) ⇒ Object

call-seq:

num ^ other -> integer

Performs bitwise XOR between num and other. Floats are truncated to integers before operation.



4142
4143
4144
# File 'lib/source/ruby.rb', line 4142

def ^(n)
  `this^n`
end

#absObject

call-seq:

num.abs -> numeric

Returns the absolute value of num.

-1234.abs   #=> 1234
1234.abs    #=> 1234


4293
4294
4295
# File 'lib/source/ruby.rb', line 4293

def abs
  `Math.abs(this)`
end

#ceilObject

call-seq:

num.ceil -> integer

Returns the smallest integer greater than or equal to num.



4302
4303
4304
# File 'lib/source/ruby.rb', line 4302

def ceil
  `Math.ceil(this)`
end

#chrObject

call-seq:

num.chr -> string

Returns a string containing the ASCII character represented by num. Floats are truncated to integers before operation.

65.chr    #=> "A"
?a.chr    #=> "a"


4315
4316
4317
# File 'lib/source/ruby.rb', line 4315

def chr
  `String.fromCharCode(parseInt(this))`
end

#coerce(n) ⇒ Object

call-seq:

num.coerce -> array

Returns an array containing num and other. This method is used by Ruby to handle mixed-type numeric operations and is included in Red for compatibility reasons only.



4326
4327
4328
# File 'lib/source/ruby.rb', line 4326

def coerce(n)
  `[this,n]`
end

#div(n) ⇒ Object

call-seq:

num./(other)   -> integer
num.div(other) -> integer

Performs division, then truncates the result to an integer. (See also Numeric#quo.)



4337
4338
4339
# File 'lib/source/ruby.rb', line 4337

def div(n)
  `parseInt(this/n)`
end

#divmodObject

FIX: Incomplete



4342
4343
# File 'lib/source/ruby.rb', line 4342

def divmod
end

#downto(limit) ⇒ Object

call-seq:

num.downto(limit) { |i| block } -> num

Iterates block, passing decreasing values from num down to and including limit, then returns num.

5.downto(1) { |n| puts "#{n}.." }    #=> 5

produces:

5..
4..
3..
2..
1..


4361
4362
4363
4364
# File 'lib/source/ruby.rb', line 4361

def downto(limit)
  `for(var i=this.valueOf();i>=limit;--i){try{#{yield `i`};}catch(e){switch(e._name){case 'next':break;case 'break':return e._value;break;case 'redo':++i;break;default:throw(e);};};}`
  return self
end

#eql?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


4366
4367
4368
# File 'lib/source/ruby.rb', line 4366

def eql?(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#equal?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


4370
4371
4372
# File 'lib/source/ruby.rb', line 4370

def equal?(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#finite?Boolean

call-seq:

num.finite? -> true or false

Returns true if num is not infinite and num.nan? is false.

Returns:

  • (Boolean)


4379
4380
4381
4382
# File 'lib/source/ruby.rb', line 4379

def finite?
  `if(this.valueOf()===Infinity||this.valueOf()===-Infinity||this.toString()==='NaN'){return false;}`
  return true
end

#floorObject

call-seq:

num.floor -> integer

Returns the largest integer less than or equal to num.



4389
4390
4391
# File 'lib/source/ruby.rb', line 4389

def floor
  `Math.floor(this)`
end

#hashObject

:nodoc:



4393
4394
4395
# File 'lib/source/ruby.rb', line 4393

def hash # :nodoc:
  `'n_'+this`
end

#id2nameObject

FIX: Incomplete



4398
4399
# File 'lib/source/ruby.rb', line 4398

def id2name
end

#infinite?Boolean

call-seq:

num.infinite? -> -1, nil, 1

Returns -1, nil, or 1 depending on whether num is -infinity, finite, or infinity.

(0).infinite?       #=> nil
(-1/0).infinite?    #=> -1
(+1/0).infinite?    #=> 1

Returns:

  • (Boolean)


4411
4412
4413
4414
4415
# File 'lib/source/ruby.rb', line 4411

def infinite?
  `if(this.valueOf()===Infinity){return 1;}`
  `if(this.valueOf()===-Infinity){return -1;}`
  return nil
end

#integer?Boolean

call-seq:

num.integer? -> true or false

Returns true if num is an integer.

Returns:

  • (Boolean)


4422
4423
4424
# File 'lib/source/ruby.rb', line 4422

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

#modulo(n) ⇒ Object

call-seq:

num % other       -> numeric
num.modulo(other) -> numeric

Returns num modulo other. See Numeric#divmod for more information.



4433
4434
4435
# File 'lib/source/ruby.rb', line 4433

def modulo(n)
  `this%n`
end

#nan?Boolean

call-seq:

num.nan? -> true or false

Returns true if num is NaN.

(0/0).nan?    #=> true

Returns:

  • (Boolean)


4444
4445
4446
# File 'lib/source/ruby.rb', line 4444

def nan?
  `this.toString()==='NaN'`
end

#nextObject

call-seq:

num.next -> integer
num.succ -> integer

Returns the integer equal to num.truncate + 1.

num = 1.5
num = num.next    #=> 2
num = num.next    #=> 3


4458
4459
4460
# File 'lib/source/ruby.rb', line 4458

def next
  `parseInt(this)+1`
end

#nonzero?Boolean

call-seq:

num.nonzero? -> num or nil

Returns num if num is not zero, nil otherwise. This behavior is useful when chaining comparisons:

a = %w(z Bb bB bb BB a aA Aa AA A)
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b   #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]

Returns:

  • (Boolean)


4472
4473
4474
# File 'lib/source/ruby.rb', line 4472

def nonzero?
  `this.valueOf()===0?nil:this`
end

#quo(n) ⇒ Object

call-seq:

num.quo(numeric) -> float

Returns the floating point result of dividing num by numeric.

654321.quo(13731)      #=> 47.6528293642124
654321.quo(13731.24)   #=> 47.6519964693647


4484
4485
4486
# File 'lib/source/ruby.rb', line 4484

def quo(n)
  `this/n`
end

#remainderObject

FIX: Incomplete



4489
4490
# File 'lib/source/ruby.rb', line 4489

def remainder
end

#roundObject

call-seq:

num.round -> integer

Rounds num to the nearest integer.



4497
4498
4499
# File 'lib/source/ruby.rb', line 4497

def round
  `Math.round(this)`
end

#step(limit, step) ⇒ Object

call-seq:

num.step(limit, step ) {|i| block } -> num

Invokes block with the sequence of numbers starting at num, incremented by step on each call, then returns self. The loop finishes when the value to be passed to the block is greater than limit (if step is positive) or less than limit (if step is negative).

1.step(5, 2) { |i| puts i }
Math::E.step(Math::PI, 0.2) { |f| puts i }

produces:

1
3
5
2.71828182845905
2.91828182845905
3.11828182845905


4520
4521
4522
4523
4524
# File 'lib/source/ruby.rb', line 4520

def step(limit, step)
  `var i=this.valueOf()`
  `if(step>0){if(i<limit){for(;limit>=i;i+=step){try{#{yield `i`};}catch(e){switch(e._name){case 'next':break;case 'break':return e._value;break;case 'redo':i-=step;break;default:throw(e);};};};};}else{if(i>limit){for(;limit<=i;i+=step){try{#{yield `i`};}catch(e){switch(e._name){case 'next':break;case 'break':return e._value;break;case 'redo':i-=step;break;default:throw(e);};}};;};}`
  return self
end

#succObject

call-seq:

num.next -> integer
num.succ -> integer

Returns the integer equal to num.truncate + 1.

num = 1.5
num = num.succ    #=> 2
num = num.succ    #=> 3


4536
4537
4538
# File 'lib/source/ruby.rb', line 4536

def succ
  `parseInt(this)+1`
end

#timesObject

call-seq:

num.times { |i| block } -> num

Iterates block num times, passing in values from zero to num - 1, then returns num.

5.times do |i|
  puts i
end

produces:

0
1
2
3
4


4558
4559
4560
4561
# File 'lib/source/ruby.rb', line 4558

def times
  `for(var i=0,l=this.valueOf();i<l;++i){try{#{yield `i`};}catch(e){switch(e._name){case 'next':break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};}}`
  return self
end

#to_fObject

call-seq:

num.to_f -> float

Returns _self_.


4568
4569
4570
# File 'lib/source/ruby.rb', line 4568

def to_f
  `this`
end

#to_iObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4579
4580
4581
# File 'lib/source/ruby.rb', line 4579

def to_i
  `parseInt(this)`
end

#to_intObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4590
4591
4592
# File 'lib/source/ruby.rb', line 4590

def to_int
  `parseInt(this)`
end

#to_s(base = 10) ⇒ Object

call-seq:

num.to_s(base = 10) -> aString

Returns a string containing the representation of num radix base (between 2 and 36).

12345.to_s       #=> "12345"
12345.to_s(2)    #=> "11000000111001"
12345.to_s(8)    #=> "30071"
12345.to_s(10)   #=> "12345"
12345.to_s(16)   #=> "3039"
12345.to_s(36)   #=> "9ix"


4607
4608
4609
# File 'lib/source/ruby.rb', line 4607

def to_s(base = 10)
  `$q(this.toString(base))`
end

#to_symObject

FIX: Incomplete



4612
4613
# File 'lib/source/ruby.rb', line 4612

def to_sym
end

#truncateObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4622
4623
4624
# File 'lib/source/ruby.rb', line 4622

def truncate
  `parseInt(this)`
end

#upto(limit) ⇒ Object

call-seq:

num.upto(limit) { |i| block } -> num

Iterates block, passing increasing values from num up to and including limit, then returns num.

98.upto(100) { |n| puts "#{n}.." }    #=> 98

produces:

98..
99..
100..


4640
4641
4642
4643
# File 'lib/source/ruby.rb', line 4640

def upto(limit)
  `for(var i=this.valueOf();i<=limit;++i){try{#{yield `i`};}catch(e){switch(e._name){case 'next':break;case 'break':return e._value;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#zero?Boolean

call-seq:

num.zero? -> true or false

Returns true if num is zero.

Returns:

  • (Boolean)


4650
4651
4652
# File 'lib/source/ruby.rb', line 4650

def zero?
  `this.valueOf()===0`
end

#|(n) ⇒ Object

call-seq:

num | other -> integer

Performs bitwise OR between num and other. Floats are truncated to integers before operation.



4132
4133
4134
# File 'lib/source/ruby.rb', line 4132

def |(n)
  `this|n`
end

#~Object

call-seq:

~num -> integer

Performs bitwise NOT on num. Floats are truncated to integers before operation.



4152
4153
4154
# File 'lib/source/ruby.rb', line 4152

def ~
  `~this`
end