Class: Integer

Inherits:
Numeric show all
Includes:
Precision

Overview

Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

Direct Known Subclasses

Bignum, Fixnum

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Precision

included, #prec, #prec_f, #prec_i

Methods inherited from Numeric

#%, #+@, #-@, #, #abs, #abs2, #angle, #arg, #class, #coerce, #conj, #conjugate, #div, #divmod, #dup, #eql?, #fdiv, #i, #imag, #imaginary, #initialize_copy, #magnitude, #modulo, #nonzero?, #phase, #polar, #quo, #real, #real?, #rect, #rectangular, #remainder, #scalar?, #singleton_method_added, #step, #to_c, #zero?

Methods included from Comparable

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

Methods inherited from NSNumber

#boolValue, #charValue, #compare:, #decimalValue, #descriptionWithLocale:, #doubleValue, #floatValue, #initWithBool:, #initWithChar:, #initWithDouble:, #initWithFloat:, #initWithInt:, #initWithInteger:, #initWithLong:, #initWithLongLong:, #initWithShort:, #initWithUnsignedChar:, #initWithUnsignedInt:, #initWithUnsignedInteger:, #initWithUnsignedLong:, #initWithUnsignedLongLong:, #initWithUnsignedShort:, #intValue, #integerValue, #isEqualToNumber:, #longLongValue, #longValue, numberWithBool:, numberWithChar:, numberWithDouble:, numberWithFloat:, numberWithInt:, numberWithInteger:, numberWithLong:, numberWithLongLong:, numberWithShort:, numberWithUnsignedChar:, numberWithUnsignedInt:, numberWithUnsignedInteger:, numberWithUnsignedLong:, numberWithUnsignedLongLong:, numberWithUnsignedShort:, #objCType, #shortValue, #stringValue, #unsignedCharValue, #unsignedIntValue, #unsignedIntegerValue, #unsignedLongLongValue, #unsignedLongValue, #unsignedShortValue

Methods inherited from NSValue

#getValue:, #initWithBytes:objCType:, #isEqualToValue:, #nonretainedObjectValue, #objCType, #pointerValue, #rangeValue, value:withObjCType:, valueWithBytes:objCType:, valueWithNonretainedObject:, valueWithPointer:, valueWithRange:

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Fixnum) induced_from(obj)

Convert obj to an Integer.

Returns:

Instance Method Details

- (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

Overloads:

- (String) chr([encoding])

Returns a string containing the character represented by the receiver's value according to encoding.

65.chr    #=> "A"
230.chr   #=> "\346"
255.chr(Encoding::UTF_8)   #=> "\303\277"

Returns:

- (1) denominator

Returns 1.

Returns:

  • (1)

- (self) downto(limit) {|i| ... } - (Enumerator) downto(limit)

Iterates block, passing decreasing values from int down to and including limit.

If no block is given, an enumerator is returned instead.

5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"

produces:

5.. 4.. 3.. 2.. 1..   Liftoff!

Overloads:

  • - downto {|i| ... }

    Yields:

    • (i)

    Returns:

    • (self)
  • - downto

    Returns:

- (Boolean) even?

Returns true if int is an even number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

Overloads:

- (Integer) gcd(int2)

Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).

For example:

2.gcd(2)                    #=> 2
3.gcd(-7)                   #=> 1
((1<<31)-1).gcd((1<<61)-1)  #=> 1

Returns:

- (Array) gcdlcm(int2)

Returns an array; [int.gcd(int2), int.lcm(int2)].

For example:

2.gcdlcm(2)                    #=> [2, 2]
3.gcdlcm(-7)                   #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1)  #=> [1, 4951760154835678088235319297]

Returns:

- (true) integer?

Always returns true.

Returns:

  • (true)

Returns:

  • (Boolean)

- (Integer) lcm(int2)

Returns the least common multiple (always positive). 0.lcm(x) and x.lcm(0) return zero.

For example:

2.lcm(2)                    #=> 2
3.lcm(-7)                   #=> 21
((1<<31)-1).lcm((1<<61)-1)  #=> 4951760154835678088235319297

Returns:

- (Integer) next - (Integer) succ

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0

Overloads:

- (self) numerator

Returns self.

Returns:

  • (self)

- (Boolean) odd?

Returns true if int is an odd number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (self) ord

Returns the int itself.

?a.ord    #=> 97

This method is intended for compatibility to character constant in Ruby 1.9. For example, ?a.ord returns 97 both in 1.8 and 1.9.

Returns:

  • (self)

- (Integer) pred

Returns the Integer equal to int - 1.

1.pred      #=> 0
(-1).pred   #=> -2

Returns:

- (Rational) rationalize([eps])

Returns the value as a rational. An optional argument eps is always ignored.

Returns:

- (Integer, Float) round([ndigits])

Rounds flt to a given precision in decimal digits (default 0 digits). Precision may be negative. Returns a floating point number when ndigits is positive, self for zero, and round down for negative.

1.round        #=> 1
1.round(2)     #=> 1.0
15.round(-1)   #=> 20

Returns:

- (Integer) next - (Integer) succ

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0

Overloads:

- (self) times {|i| ... } - (Enumerator) times

Iterates block int times, passing in values from zero to int - 1.

If no block is given, an enumerator is returned instead.

5.times do |i|
  print i, " "
end

produces:

0 1 2 3 4

Overloads:

  • - times {|i| ... }

    Yields:

    • (i)

    Returns:

    • (self)
  • - times

    Returns:

- (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

Overloads:

- (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

Overloads:

- (Rational) to_r

Returns the value as a rational.

For example:

1.to_r        #=> (1/1)
(1<<64).to_r  #=> (18446744073709551616/1)

Returns:

- (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

Overloads:

- (self) upto(limit) {|i| ... } - (Enumerator) upto(limit)

Iterates block, passing in integer values from int up to and including limit.

If no block is given, an enumerator is returned instead.

5.upto(10) { |i| print i, " " }

produces:

5 6 7 8 9 10

Overloads:

  • - upto {|i| ... }

    Yields:

    • (i)

    Returns:

    • (self)
  • - upto

    Returns: