📜 ⬆️ ⬇️

Ruby integers

Ruby supports integers and floating point numbers (float numbers). Integers can be of any order (in fact, they are limited on top by the amount of memory allocated by your operating system). Integers within the range (-2 ^ 30, 2 ^ 30 - 1) or (-2 ^ 62, 2 ^ 62 -1) are stored in binary form and are instances of the class Fixnum . Integers outside these ranges are stored as instances of the Bignum class (at the moment the class is a variable length set whose elements are instances of the class Fixnum ). This difference is transparent to the user and Ruby automatically converts classes to both sides.

num = 81
6.times do
puts "# {num.class}: # {num}"
num * = num
end

will give
Fixnum: 81
Fixnum: 6561
Fixnum: 43046721
Bignum: 1853020188851841
Bignum: 3433683820292512484657849089281
Bignum: 11790184577738583171520872861412518665678211592275841109096961

To write an integer literal, you optionally use the sign (" + " or " - "), an optional indicator indicating what form the number should be written in (see example: 0 for octal, 0d for decimal — the default is 0x for hex binary), then write a string consisting of numbers. Underscores are ignored in this line, and some guys use them instead of a comma in higher numbers.
123456 => 123456 # Fixnum
0d123456 => 123456 # Fixnum
123_456 => 123456 # Fixnum underscores are ignored
-543 => 543 # Fixnum negative number
0xaabb => 43707 # Fixnum hexadecimal form
0377 => 255 # Fixnum octal form
0b10_1010 => 42 # Fixnum binary form with minus sign
123_456_789_123_456_789 => 123456789123456789 # Bignum

You can generate control characters (carriage return, for example) using the form ? \ Cx and ? \ Cx . Metacharacters ( x | 0x80 ) can be generated using ? \ Mx . Their combination is generated using ? \ M- \ Cx . The backslash number can be obtained using the entry ? \\ .
? a => 97 # ASCII character
? \ n => 10 # code for the new line (0x0a)
? \ Ca => 1 # CTRL a =? A & 0x9f = 0x01
? \ Ma => 225 # meta sets bit 7
? \ M- \ Ca => 129 # meta and control a
? \ C-? => 127 # delete character

A numeric literal with a decimal point and / or exponent corresponds to an object of class Float , which in turn corresponds to the native type double operating system. After the dot, there must be a number, for example, if you write the number 1.0e3 in the form 1.e3 , Ruby will try to call the e3 method of the Fixnum class.
All numbers are objects and respond to a large number of messages. This is different from, for example, C ++: the module of the number should be found in the num.abs method, not abs (num) .
Integers support some useful iterators. Their names speak for themselves: 6.times calls the block of code associated with it 6 times, upto and downto are needed to set the upper or lower limit for the number of calls to the block of code. The Numeric class provides a generic step method, more similar to all the usual for .
3.times {print "X"}
1.upto (5) {| i | print i, ""}
99.downto (95) {| i | print i, ""}
50.step (80, 5) {| i | print i, ""}

will give
XXX 1 2 3 4 5 99 98 97 96 95 50 55 60 65 70 75 80

Finally, you need to warn Perl programmers (and some others too - approx. Transl.): Strings that contain numbers are not automatically converted into numbers. This can let you down if you read numbers from a file. For example, we want to find the sum of the numbers on each line from the file:
3 4
5 6
7 8

The following code will give an unexpected result for someone:
some_file.each do | line |
v1, v2 = line.split # split a line into objects between spaces
print v1 + v2, ""
end

will give us
34 56 78

The problem is that v1 and v2 contain references to string objects. For them, the result of the addition operation is the concatenation of two strings. To achieve the desired result, use the Integer method to convert a string to an integer.
some_file.each do | line |
v1, v2 = line.split # split a line into objects between spaces
print Integer (v1) + Integer (v2), ""
end

finally gives us:
7 11 15

Hooray!

Dave Thomas Book Translation - Programming Ruby .

')

Source: https://habr.com/ru/post/31535/


All Articles