Ruby Numbers
Working with numeric data in Ruby
🔢 What are Ruby Numbers?
Ruby supports various numeric types including integers and floating-point numbers. Numbers can be used for calculations, comparisons, and mathematical operations. Ruby handles both small and arbitrarily large numbers automatically.
# Different number types
age = 25 # Integer
price = 19.99 # Float
large = 1_000_000 # Integer with underscores
puts age + 5 # Output: 30
puts price * 2 # Output: 39.98
Number Types
Integers
Whole numbers
count = 42
year = 2024
Floats
Decimal numbers
price = 19.99
pi = 3.14159
Operations
Mathematical calculations
10 + 5 # 15
10 * 5 # 50
Conversion
Change number types
42.to_f # 42.0
3.14.to_i # 3
🔹 Integer Numbers
Integers are whole numbers without decimal points. Ruby automatically handles both small and very large integers efficiently.
# Integer examples
age = 25
year = 2024
negative = -10
large = 1_000_000_000 # Underscores for readability
puts age # Output: 25
puts negative # Output: -10
puts large # Output: 1000000000
puts age.class # Output: Integer
Output:
25 -10 1000000000 Integer
🔹 Floating-Point Numbers
Floats represent decimal numbers. They're essential for precise calculations involving fractions and measurements.
# Float examples
price = 19.99
temperature = -5.5
pi = 3.14159
scientific = 1.5e3 # Scientific notation (1500.0)
puts price # Output: 19.99
puts temperature # Output: -5.5
puts scientific # Output: 1500.0
puts price.class # Output: Float
Output:
19.99 -5.5 1500.0 Float
🔹 Basic Arithmetic Operations
Perform mathematical calculations using standard arithmetic operators. Ruby follows standard mathematical precedence rules.
# Arithmetic operations
a = 20
b = 5
puts a + b # Addition: 25
puts a - b # Subtraction: 15
puts a * b # Multiplication: 100
puts a / b # Division: 4
puts a % b # Modulus (remainder): 0
puts a ** b # Exponentiation: 3200000
Output:
25 15 100 4 0 3200000
🔹 Integer vs Float Division
Division behavior differs between integers and floats. Integer division returns an integer, while float division returns a decimal result.
# Integer division
puts 10 / 3 # Output: 3 (integer result)
puts 10 / 4 # Output: 2
# Float division
puts 10.0 / 3 # Output: 3.3333333333333335
puts 10 / 3.0 # Output: 3.3333333333333335
puts 10.0 / 4.0 # Output: 2.5
# Convert to float for decimal result
puts 10.to_f / 3 # Output: 3.3333333333333335
Output:
3 2 3.3333333333333335 3.3333333333333335 2.5 3.3333333333333335
🔹 Number Conversion
Convert between different numeric types using conversion methods. Ruby provides simple methods for type conversion.
# Integer to Float
num = 42
puts num.to_f # Output: 42.0
# Float to Integer (truncates decimal)
price = 19.99
puts price.to_i # Output: 19
# String to Number
text = "100"
puts text.to_i # Output: 100
puts text.to_f # Output: 100.0
# Number to String
age = 25
puts age.to_s # Output: "25"
Output:
42.0 19 100 100.0 25
🔹 Number Methods
Ruby provides many useful methods for working with numbers, including rounding, absolute values, and mathematical operations.
# Useful number methods
num = -15.7
puts num.abs # Absolute value: 15.7
puts num.round # Round: -16
puts num.floor # Floor: -16
puts num.ceil # Ceiling: -15
# Even and odd
puts 10.even? # Output: true
puts 10.odd? # Output: false
puts 7.even? # Output: false
puts 7.odd? # Output: true
Output:
15.7 -16 -16 -15 true false false true
🔹 Number Ranges
Create sequences of numbers using ranges. Ranges are useful for loops and generating number sequences.
# Number ranges
range1 = (1..5) # Inclusive (includes 5)
range2 = (1...5) # Exclusive (excludes 5)
puts range1.to_a.inspect # Output: [1, 2, 3, 4, 5]
puts range2.to_a.inspect # Output: [1, 2, 3, 4]
# Using ranges in loops
(1..3).each do |i|
puts i
end
# Output: 1, 2, 3
Output:
[1, 2, 3, 4, 5] [1, 2, 3, 4] 1 2 3
🔹 Random Numbers
Generate random numbers using the rand method. Useful for games, simulations, and random selections.
# Random numbers
puts rand # Random float between 0 and 1
puts rand(10) # Random integer from 0 to 9
puts rand(1..6) # Random integer from 1 to 6 (dice roll)
# Random float in range
puts rand * 100 # Random float from 0 to 100
# Example: Dice roll
dice = rand(1..6)
puts "You rolled: #{dice}"
Output (example):
0.6789 7 4 45.23 You rolled: 3
🔹 Math Module
Ruby's Math module provides advanced mathematical functions like square root, trigonometry, and logarithms.
# Math module functions
puts Math.sqrt(16) # Square root: 4.0
puts Math.sqrt(2) # Output: 1.4142135623730951
puts Math::PI # Pi constant: 3.141592653589793
puts Math::E # Euler's number: 2.718281828459045
# Trigonometry
puts Math.sin(0) # Output: 0.0
puts Math.cos(0) # Output: 1.0
# Power and logarithm
puts Math.log(10) # Natural log: 2.302585092994046
puts Math.log10(100) # Base 10 log: 2.0
Output:
4.0 1.4142135623730951 3.141592653589793 2.718281828459045 0.0 1.0 2.302585092994046 2.0
💡 Number Quick Reference:
- to_i: Convert to integer
- to_f: Convert to float
- abs: Absolute value
- round: Round to nearest integer
- ceil: Round up
- floor: Round down
- even?/odd?: Check if even or odd
- rand: Generate random number