Class: Numeric
Overview
Numeric
objects represent native JavaScript numbers. Class Numeric
subsumes the methods of Ruby’s Float
, Integer
, Bignum
, and Fixnum
.
Instance Method Summary collapse
-
#%(n) ⇒ Object
call-seq: num % other -> numeric num.modulo(other) -> numeric.
-
#&(n) ⇒ Object
call-seq: num & other -> integer.
-
#*(n) ⇒ Object
call-seq: num * other -> numeric.
-
#**(n) ⇒ Object
call-seq: num ** n -> numeric.
-
#+(n) ⇒ Object
call-seq: num + other -> numeric.
-
#+@ ⇒ Object
call-seq: +num -> num.
-
#-(n) ⇒ Object
call-seq: num - other -> numeric.
-
#-@ ⇒ Object
call-seq: -num -> numeric.
-
#/(n) ⇒ Object
call-seq: num / other -> integer num.div(other) -> integer.
-
#<<(n) ⇒ Object
call-seq: num << n -> integer.
-
#<=>(n) ⇒ Object
call-seq: num <=> numeric -> -1, 0, 1.
-
#==(n) ⇒ Object
:nodoc:.
-
#===(n) ⇒ Object
:nodoc:.
-
#>>(n) ⇒ Object
call-seq: num << n -> integer.
-
#[](n) ⇒ Object
call-seq: num -> 0, 1.
-
#^(n) ⇒ Object
call-seq: num ^ other -> integer.
-
#abs ⇒ Object
call-seq: num.abs -> numeric.
-
#ceil ⇒ Object
call-seq: num.ceil -> integer.
-
#chr ⇒ Object
call-seq: num.chr -> string.
-
#coerce(n) ⇒ Object
call-seq: num.coerce -> array.
-
#div(n) ⇒ Object
call-seq: num./(other) -> integer num.div(other) -> integer.
-
#divmod ⇒ Object
FIX: Incomplete.
-
#downto(limit) ⇒ Object
call-seq: num.downto(limit) { |i| block } -> num.
-
#eql?(n) ⇒ Boolean
:nodoc:.
-
#equal?(n) ⇒ Boolean
:nodoc:.
-
#finite? ⇒ Boolean
call-seq: num.finite? -> true or false.
-
#floor ⇒ Object
call-seq: num.floor -> integer.
-
#hash ⇒ Object
:nodoc:.
-
#id2name ⇒ Object
FIX: Incomplete.
-
#infinite? ⇒ Boolean
call-seq: num.infinite? -> -1, nil, 1.
-
#integer? ⇒ Boolean
call-seq: num.integer? -> true or false.
-
#modulo(n) ⇒ Object
call-seq: num % other -> numeric num.modulo(other) -> numeric.
-
#nan? ⇒ Boolean
call-seq: num.nan? -> true or false.
-
#next ⇒ Object
call-seq: num.next -> integer num.succ -> integer.
-
#nonzero? ⇒ Boolean
call-seq: num.nonzero? -> num or nil.
-
#quo(n) ⇒ Object
call-seq: num.quo(numeric) -> float.
-
#remainder ⇒ Object
FIX: Incomplete.
-
#round ⇒ Object
call-seq: num.round -> integer.
-
#step(limit, step) ⇒ Object
call-seq: num.step(limit, step ) {|i| block } -> num.
-
#succ ⇒ Object
call-seq: num.next -> integer num.succ -> integer.
-
#times ⇒ Object
call-seq: num.times { |i| block } -> num.
-
#to_f ⇒ Object
call-seq: num.to_f -> float.
-
#to_i ⇒ Object
call-seq: num.to_i -> integer num.to_int -> integer num.truncate -> integer.
-
#to_int ⇒ Object
call-seq: num.to_i -> integer num.to_int -> integer num.truncate -> integer.
-
#to_s(base = 10) ⇒ Object
call-seq: num.to_s(base = 10) -> aString.
-
#to_sym ⇒ Object
FIX: Incomplete.
-
#truncate ⇒ Object
call-seq: num.to_i -> integer num.to_int -> integer num.truncate -> integer.
-
#upto(limit) ⇒ Object
call-seq: num.upto(limit) { |i| block } -> num.
-
#zero? ⇒ Boolean
call-seq: num.zero? -> true or false.
-
#|(n) ⇒ Object
call-seq: num | other -> integer.
-
#~ ⇒ Object
call-seq: ~num -> integer.
Methods included from Comparable
Instance Method Details
#%(n) ⇒ Object
call-seq:
num % other -> numeric
num.modulo(other) -> numeric
Returns num modulo other. See Numeric#divmod
for more information.
4164 4165 4166 |
# File 'lib/source/ruby.rb', line 4164 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.
4174 4175 4176 |
# File 'lib/source/ruby.rb', line 4174 def &(n) `this&n` end |
#*(n) ⇒ Object
call-seq:
num * other -> numeric
Multiplies num by other, then returns the result.
4224 4225 4226 |
# File 'lib/source/ruby.rb', line 4224 def *(n) `this*n` end |
#**(n) ⇒ Object
call-seq:
num ** n -> numeric
Raises num to the nth power, which may be negative or fractional.
4233 4234 4235 |
# File 'lib/source/ruby.rb', line 4233 def **(n) `Math.pow(this,n)` end |
#+(n) ⇒ Object
call-seq:
num + other -> numeric
Adds num to other, then returns the result.
4242 4243 4244 |
# File 'lib/source/ruby.rb', line 4242 def +(n) `this+n` end |
#+@ ⇒ Object
call-seq:
+num -> num
Unary Plus – returns the value of num.
4251 4252 4253 |
# File 'lib/source/ruby.rb', line 4251 def +@() `this` end |
#-(n) ⇒ Object
call-seq:
num - other -> numeric
Subtracts other from num, then returns the result.
4260 4261 4262 |
# File 'lib/source/ruby.rb', line 4260 def -(n) `this-n` end |
#-@ ⇒ Object
call-seq:
-num -> numeric
Unary Minus – returns the value of num negated.
4269 4270 4271 |
# File 'lib/source/ruby.rb', line 4269 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
.)
4215 4216 4217 |
# File 'lib/source/ruby.rb', line 4215 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.
4279 4280 4281 |
# File 'lib/source/ruby.rb', line 4279 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
.
4300 4301 4302 4303 4304 4305 |
# File 'lib/source/ruby.rb', line 4300 def <=>(n) `if(n.constructor!=Number){return nil;}` `if(this>n){return 1;}` `if(this<n){return -1;}` return 0 end |
#==(n) ⇒ Object
:nodoc:
4307 4308 4309 |
# File 'lib/source/ruby.rb', line 4307 def ==(n) # :nodoc: `this.valueOf()===n.valueOf()` end |
#===(n) ⇒ Object
:nodoc:
4311 4312 4313 |
# File 'lib/source/ruby.rb', line 4311 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.
4289 4290 4291 |
# File 'lib/source/ruby.rb', line 4289 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
4331 4332 4333 4334 4335 |
# File 'lib/source/ruby.rb', line 4331 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.
4194 4195 4196 |
# File 'lib/source/ruby.rb', line 4194 def ^(n) `this^n` end |
#abs ⇒ Object
call-seq:
num.abs -> numeric
Returns the absolute value of num.
-1234.abs #=> 1234
1234.abs #=> 1234
4345 4346 4347 |
# File 'lib/source/ruby.rb', line 4345 def abs `Math.abs(this)` end |
#ceil ⇒ Object
call-seq:
num.ceil -> integer
Returns the smallest integer greater than or equal to num.
4354 4355 4356 |
# File 'lib/source/ruby.rb', line 4354 def ceil `Math.ceil(this)` end |
#chr ⇒ Object
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"
4367 4368 4369 |
# File 'lib/source/ruby.rb', line 4367 def chr `$q(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.
4378 4379 4380 |
# File 'lib/source/ruby.rb', line 4378 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
.)
4389 4390 4391 |
# File 'lib/source/ruby.rb', line 4389 def div(n) `parseInt(this/n)` 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..
4413 4414 4415 4416 |
# File 'lib/source/ruby.rb', line 4413 def downto(limit) `for(var i=this.valueOf();i>=limit;--i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':++i;break;default:throw(e);};};}` return self end |
#eql?(n) ⇒ Boolean
:nodoc:
4418 4419 4420 |
# File 'lib/source/ruby.rb', line 4418 def eql?(n) # :nodoc: `this.valueOf()===n.valueOf()` end |
#equal?(n) ⇒ Boolean
:nodoc:
4422 4423 4424 |
# File 'lib/source/ruby.rb', line 4422 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.
4431 4432 4433 4434 |
# File 'lib/source/ruby.rb', line 4431 def finite? `if(this.valueOf()===Infinity||this.valueOf()===-Infinity||this.toString()==='NaN'){return false;}` return true end |
#floor ⇒ Object
call-seq:
num.floor -> integer
Returns the largest integer less than or equal to num.
4441 4442 4443 |
# File 'lib/source/ruby.rb', line 4441 def floor `Math.floor(this)` end |
#hash ⇒ Object
:nodoc:
4445 4446 4447 |
# File 'lib/source/ruby.rb', line 4445 def hash # :nodoc: `'n_'+this` 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
4463 4464 4465 4466 4467 |
# File 'lib/source/ruby.rb', line 4463 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.
4474 4475 4476 |
# File 'lib/source/ruby.rb', line 4474 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.
4485 4486 4487 |
# File 'lib/source/ruby.rb', line 4485 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
4496 4497 4498 |
# File 'lib/source/ruby.rb', line 4496 def nan? `this.toString()==='NaN'` end |
#next ⇒ Object
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
4510 4511 4512 |
# File 'lib/source/ruby.rb', line 4510 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"]
4524 4525 4526 |
# File 'lib/source/ruby.rb', line 4524 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
4536 4537 4538 |
# File 'lib/source/ruby.rb', line 4536 def quo(n) `this/n` end |
#remainder ⇒ Object
FIX: Incomplete
4541 4542 |
# File 'lib/source/ruby.rb', line 4541 def remainder end |
#round ⇒ Object
call-seq:
num.round -> integer
Rounds num to the nearest integer.
4549 4550 4551 |
# File 'lib/source/ruby.rb', line 4549 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
4572 4573 4574 4575 4576 |
# File 'lib/source/ruby.rb', line 4572 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.__keyword__){case 'next':break;case 'break':return e.__return__;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.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':i-=step;break;default:throw(e);};}};;};}` return self end |
#succ ⇒ Object
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
4588 4589 4590 |
# File 'lib/source/ruby.rb', line 4588 def succ `parseInt(this)+1` end |
#times ⇒ Object
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
4610 4611 4612 4613 |
# File 'lib/source/ruby.rb', line 4610 def times `for(var i=0,l=this.valueOf();i<l;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};}}` return self end |
#to_f ⇒ Object
call-seq:
num.to_f -> float
Returns _self_.
4620 4621 4622 |
# File 'lib/source/ruby.rb', line 4620 def to_f `this` end |
#to_i ⇒ Object
call-seq:
num.to_i -> integer
num.to_int -> integer
num.truncate -> integer
Returns num truncated to an integer.
4631 4632 4633 |
# File 'lib/source/ruby.rb', line 4631 def to_i `parseInt(this)` end |
#to_int ⇒ Object
call-seq:
num.to_i -> integer
num.to_int -> integer
num.truncate -> integer
Returns num truncated to an integer.
4642 4643 4644 |
# File 'lib/source/ruby.rb', line 4642 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"
4659 4660 4661 |
# File 'lib/source/ruby.rb', line 4659 def to_s(base = 10) `$q(this.toString(base))` end |
#truncate ⇒ Object
call-seq:
num.to_i -> integer
num.to_int -> integer
num.truncate -> integer
Returns num truncated to an integer.
4674 4675 4676 |
# File 'lib/source/ruby.rb', line 4674 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..
4692 4693 4694 4695 |
# File 'lib/source/ruby.rb', line 4692 def upto(limit) `for(var i=this.valueOf();i<=limit;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;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.
4702 4703 4704 |
# File 'lib/source/ruby.rb', line 4702 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.
4184 4185 4186 |
# File 'lib/source/ruby.rb', line 4184 def |(n) `this|n` end |
#~ ⇒ Object
call-seq:
~num -> integer
Performs bitwise NOT
on num. Floats are truncated to integers before operation.
4204 4205 4206 |
# File 'lib/source/ruby.rb', line 4204 def ~ `~this` end |