553
Arrays in Ruby are ordered collections of objects, which can include numbers, strings, or other objects.
They are versatile and support various methods to manipulate and access their elements.
What You’ll Learn
1. Introduction to Arrays
An array is a list of elements indexed starting from 0. Elements in an array can be of any data type, and Ruby arrays allow elements of mixed types.
array = [1, "Ruby", true, 3.14]
2. Creating Arrays
2.1 Using Square Brackets
array = [1, 2, 3, 4] puts array.inspect # Output: [1, 2, 3, 4]
2.2 Using Array.new
# Empty array empty_array = Array.new puts empty_array.inspect # Output: [] # Array with default size and values array_with_defaults = Array.new(5, "Ruby") puts array_with_defaults.inspect # Output: ["Ruby", "Ruby", "Ruby", "Ruby", "Ruby"]
2.3 Using a Block with Array.new
array = Array.new(5) { |index| index * 2 } puts array.inspect # Output: [0, 2, 4, 6, 8]
3. Accessing Elements
3.1 Access by Index
array = [10, 20, 30, 40, 50] puts array[0] # Output: 10 puts array[2] # Output: 30 puts array[-1] # Output: 50 (last element)
3.2 Access Ranges
array = [10, 20, 30, 40, 50] puts array[1..3].inspect # Output: [20, 30, 40] puts array[1...3].inspect # Output: [20, 30]
3.3 Access Using at
array = [10, 20, 30, 40] puts array.at(2) # Output: 30
4. Array Operations
4.1 Adding Elements
array = [1, 2, 3] array << 4 # Append puts array.inspect # Output: [1, 2, 3, 4] array.push(5) # Push an element puts array.inspect # Output: [1, 2, 3, 4, 5] array.unshift(0) # Add to the beginning puts array.inspect # Output: [0, 1, 2, 3, 4, 5]
4.2 Removing Elements
array = [1, 2, 3, 4, 5] array.pop # Remove the last element puts array.inspect # Output: [1, 2, 3, 4] array.shift # Remove the first element puts array.inspect # Output: [2, 3, 4] array.delete(3) # Remove a specific element puts array.inspect # Output: [2, 4]
4.3 Combining Arrays
array1 = [1, 2] array2 = [3, 4] combined = array1 + array2 puts combined.inspect # Output: [1, 2, 3, 4]
4.4 Subtracting Arrays
array1 = [1, 2, 3, 4] array2 = [2, 4] difference = array1 - array2 puts difference.inspect # Output: [1, 3]
5. Common Array Methods
5.1 Iterating Over Arrays
array = [10, 20, 30] array.each { |element| puts element } # Output: # 10 # 20 # 30
5.2 Map (Transform Elements)
array = [1, 2, 3] squared = array.map { |x| x ** 2 } puts squared.inspect # Output: [1, 4, 9]
5.3 Filtering Elements
array = [1, 2, 3, 4, 5] even_numbers = array.select { |x| x.even? } puts even_numbers.inspect # Output: [2, 4]
5.4 Checking for Inclusion
array = [1, 2, 3, 4] puts array.include?(3) # Output: true puts array.include?(5) # Output: false
5.5 Sorting Arrays
array = [5, 2, 8, 1] sorted = array.sort puts sorted.inspect # Output: [1, 2, 5, 8]
5.6 Removing Duplicates
array = [1, 2, 2, 3, 3, 3] unique = array.uniq puts unique.inspect # Output: [1, 2, 3]
6. Nested Arrays
Ruby supports arrays within arrays (nested arrays).
Example
nested_array = [[1, 2], [3, 4], [5, 6]] # Access elements puts nested_array[1].inspect # Output: [3, 4] puts nested_array[1][0] # Output: 3 # Flatten the nested array flattened = nested_array.flatten puts flattened.inspect # Output: [1, 2, 3, 4, 5, 6]
7. Practical Examples
7.1 Find the Maximum and Minimum
array = [10, 20, 5, 15] puts array.max # Output: 20 puts array.min # Output: 5
7.2 Remove Nil Values
array = [1, nil, 2, nil, 3] filtered = array.compact puts filtered.inspect # Output: [1, 2, 3]
7.3 Find the Index of an Element
array = [10, 20, 30, 40] puts array.index(30) # Output: 2
7.4 Split a String into an Array
string = "Ruby is fun" words = string.split(" ") puts words.inspect # Output: ["Ruby", "is", "fun"]
7.5 Convert an Array to a String
array = ["Ruby", "is", "fun"] sentence = array.join(" ") puts sentence # Output: Ruby is fun
7.6 Find Common Elements
array1 = [1, 2, 3] array2 = [2, 3, 4] common = array1 & array2 puts common.inspect # Output: [2, 3]
8. Summary
Key Points
- Arrays in Ruby are dynamic and can store mixed data types.
- Ruby provides a rich set of methods to manipulate arrays effectively.
- Nested arrays allow you to represent complex data structures.
Best Practices
- Use meaningful names for arrays to improve code readability.
- Avoid modifying arrays in-place unless necessary.
- Take advantage of Ruby’s expressive methods for filtering, mapping, and iterating over arrays.