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.
4135 4136 4137 |
# File 'lib/source/ruby.rb', line 4135 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.
4145 4146 4147 |
# File 'lib/source/ruby.rb', line 4145 def &(n) `this&n` end |
#*(n) ⇒ Object
call-seq:
num * other -> numeric
Multiplies num by other, then returns the result.
4195 4196 4197 |
# File 'lib/source/ruby.rb', line 4195 def *(n) `this*n` end |
#**(n) ⇒ Object
call-seq:
num ** n -> numeric
Raises num to the nth power, which may be negative or fractional.
4204 4205 4206 |
# File 'lib/source/ruby.rb', line 4204 def **(n) `Math.pow(this,n)` end |
#+(n) ⇒ Object
call-seq:
num + other -> numeric
Adds num to other, then returns the result.
4213 4214 4215 |
# File 'lib/source/ruby.rb', line 4213 def +(n) `this+n` end |
#+@ ⇒ Object
call-seq:
+num -> num
Unary Plus – returns the value of num.
4222 4223 4224 |
# File 'lib/source/ruby.rb', line 4222 def +@() `this` end |
#-(n) ⇒ Object
call-seq:
num - other -> numeric
Subtracts other from num, then returns the result.
4231 4232 4233 |
# File 'lib/source/ruby.rb', line 4231 def -(n) `this-n` end |
#-@ ⇒ Object
call-seq:
-num -> numeric
Unary Minus – returns the value of num negated.
4240 4241 4242 |
# File 'lib/source/ruby.rb', line 4240 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
.)
4186 4187 4188 |
# File 'lib/source/ruby.rb', line 4186 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.
4250 4251 4252 |
# File 'lib/source/ruby.rb', line 4250 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
.
4271 4272 4273 4274 4275 4276 |
# File 'lib/source/ruby.rb', line 4271 def <=>(n) `if(n.constructor!=Number){return nil;}` `if(this>n){return 1;}` `if(this<n){return -1;}` return 0 end |
#==(n) ⇒ Object
:nodoc:
4278 4279 4280 |
# File 'lib/source/ruby.rb', line 4278 def ==(n) # :nodoc: `this.valueOf()===n.valueOf()` end |
#===(n) ⇒ Object
:nodoc:
4282 4283 4284 |
# File 'lib/source/ruby.rb', line 4282 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.
4260 4261 4262 |
# File 'lib/source/ruby.rb', line 4260 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
4302 4303 4304 4305 4306 |
# File 'lib/source/ruby.rb', line 4302 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.
4165 4166 4167 |
# File 'lib/source/ruby.rb', line 4165 def ^(n) `this^n` end |
#abs ⇒ Object
call-seq:
num.abs -> numeric
Returns the absolute value of num.
-1234.abs #=> 1234
1234.abs #=> 1234
4316 4317 4318 |
# File 'lib/source/ruby.rb', line 4316 def abs `Math.abs(this)` end |
#ceil ⇒ Object
call-seq:
num.ceil -> integer
Returns the smallest integer greater than or equal to num.
4325 4326 4327 |
# File 'lib/source/ruby.rb', line 4325 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"
4338 4339 4340 |
# File 'lib/source/ruby.rb', line 4338 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.
4349 4350 4351 |
# File 'lib/source/ruby.rb', line 4349 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
.)
4360 4361 4362 |
# File 'lib/source/ruby.rb', line 4360 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..
4384 4385 4386 4387 |
# File 'lib/source/ruby.rb', line 4384 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._value;break;case 'redo':++i;break;default:throw(e);};};}` return self end |
#eql?(n) ⇒ Boolean
:nodoc:
4389 4390 4391 |
# File 'lib/source/ruby.rb', line 4389 def eql?(n) # :nodoc: `this.valueOf()===n.valueOf()` end |
#equal?(n) ⇒ Boolean
:nodoc:
4393 4394 4395 |
# File 'lib/source/ruby.rb', line 4393 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.
4402 4403 4404 4405 |
# File 'lib/source/ruby.rb', line 4402 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.
4412 4413 4414 |
# File 'lib/source/ruby.rb', line 4412 def floor `Math.floor(this)` end |
#hash ⇒ Object
:nodoc:
4416 4417 4418 |
# File 'lib/source/ruby.rb', line 4416 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
4434 4435 4436 4437 4438 |
# File 'lib/source/ruby.rb', line 4434 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.
4445 4446 4447 |
# File 'lib/source/ruby.rb', line 4445 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.
4456 4457 4458 |
# File 'lib/source/ruby.rb', line 4456 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
4467 4468 4469 |
# File 'lib/source/ruby.rb', line 4467 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
4481 4482 4483 |
# File 'lib/source/ruby.rb', line 4481 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"]
4495 4496 4497 |
# File 'lib/source/ruby.rb', line 4495 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
4507 4508 4509 |
# File 'lib/source/ruby.rb', line 4507 def quo(n) `this/n` end |
#remainder ⇒ Object
FIX: Incomplete
4512 4513 |
# File 'lib/source/ruby.rb', line 4512 def remainder end |
#round ⇒ Object
call-seq:
num.round -> integer
Rounds num to the nearest integer.
4520 4521 4522 |
# File 'lib/source/ruby.rb', line 4520 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
4543 4544 4545 4546 4547 |
# File 'lib/source/ruby.rb', line 4543 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._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.__keyword__){case 'next':break;case 'break':return e._value;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
4559 4560 4561 |
# File 'lib/source/ruby.rb', line 4559 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
4581 4582 4583 4584 |
# File 'lib/source/ruby.rb', line 4581 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._value;break;case 'redo':--i;break;default:throw(e);};}}` return self end |
#to_f ⇒ Object
call-seq:
num.to_f -> float
Returns _self_.
4591 4592 4593 |
# File 'lib/source/ruby.rb', line 4591 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.
4602 4603 4604 |
# File 'lib/source/ruby.rb', line 4602 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.
4613 4614 4615 |
# File 'lib/source/ruby.rb', line 4613 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"
4630 4631 4632 |
# File 'lib/source/ruby.rb', line 4630 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.
4645 4646 4647 |
# File 'lib/source/ruby.rb', line 4645 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..
4663 4664 4665 4666 |
# File 'lib/source/ruby.rb', line 4663 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._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.
4673 4674 4675 |
# File 'lib/source/ruby.rb', line 4673 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.
4155 4156 4157 |
# File 'lib/source/ruby.rb', line 4155 def |(n) `this|n` end |
#~ ⇒ Object
call-seq:
~num -> integer
Performs bitwise NOT
on num. Floats are truncated to integers before operation.
4175 4176 4177 |
# File 'lib/source/ruby.rb', line 4175 def ~ `~this` end |