Tutorial by Examples

0 # creates the Fixnum 0 123 # creates the Fixnum 123 1_000 # creates the Fixnum 1000. You can use _ as separator for readability By default the notation is base 10. However, there are some other built-in notations for different bases: 0xFF # Hexadecimal representation of 255, s...
You can use the Integer method to convert a String to an Integer: Integer("123") # => 123 Integer("0xFF") # => 255 Integer("0b100") # => 4 Integer("0555") # => 365 You can also pass a base parameter to the Integer method to c...
Fixnum#to_s takes an optional base argument and represents the given number in that base: 2.to_s(2) # => "10" 3.to_s(2) # => "11" 3.to_s(3) # => "10" 10.to_s(16) # => "a" If no argument is provided, then it represents the number in bas...
When dividing two numbers pay attention to the type you want in return. Note that dividing two integers will invoke the integer division. If your goal is to run the float division, at least one of the parameters should be of float type. Integer division: 3 / 2 # => 1 Float division 3 / 3.0 ...
Rational represents a rational number as numerator and denominator: r1 = Rational(2, 3) r2 = 2.5.to_r r3 = r1 + r2 r3.numerator # => 19 r3.denominator # => 6 Rational(2, 4) # => (1/2) Other ways of creating a Rational Rational('2/3') # => (2/3) Rational(3) # => (3/1...
1i # => (0+1i) 1.to_c # => (1+0i) rectangular = Complex(2, 3) # => (2+3i) polar = Complex('1@2') # => (-0.4161468365471424+0.9092974268256817i) polar.rectangular # => [-0.4161468365471424, 0.9092974268256817] rectangular.polar # => [3.605551275463989, 0.9827937232...
The even? method can be used to determine if a number is even 4.even? # => true 5.even? # => false The odd? method can be used to determine if a number is odd 4.odd? # => false 5.odd? # => true
The round method will round a number up if the first digit after its decimal place is 5 or higher and round down if that digit is 4 or lower. This takes in an optional argument for the precision you're looking for. 4.89.round # => 5 4.25.round # => 4 3.141526.round(1) # => ...

Page 1 of 1