0
0
Rubyprogramming~5 mins

Array slicing and ranges in Ruby

Choose your learning style9 modes available
Introduction
Array slicing lets you take parts of a list easily. Ranges help pick a group of items by their positions.
You want to get the first few items from a list, like the first 3 fruits.
You want to get the last few items, like the last 2 days of the week.
You want to get a middle part, like days 2 to 4 from a list of days.
You want to loop over a part of an array without changing the original.
You want to check or change a section of an array quickly.
Syntax
Ruby
class Array
  # Get a slice using a range
  def slice(range)
    # returns a new array with elements from range
  end
end

# Using range to slice
array[start_index..end_index]  # inclusive range
array[start_index...end_index] # exclusive range

# Using slice method
array.slice(range)

# Using slice with start and length
array.slice(start_index, length)
Ranges use two dots (..) to include the end index, and three dots (...) to exclude it.
Array indices start at 0 in Ruby.
Examples
Shows how to use inclusive and exclusive ranges to get parts of the array.
Ruby
fruits = ['apple', 'banana', 'cherry', 'date', 'fig']

# Get first 3 fruits
first_three = fruits[0..2]

# Get fruits from index 2 to 4
middle_fruits = fruits[2..4]

# Get fruits from index 1 to 3 (excluding 3)
partial_fruits = fruits[1...3]
Shows slice with start and length, and edge cases with empty and single element arrays.
Ruby
numbers = [10, 20, 30, 40, 50]

# Slice with start and length
slice_part = numbers.slice(1, 3)  # gets elements at index 1,2,3

# Edge case: empty array
empty = [].slice(0..2)  # returns []

# Edge case: single element array
single = [99].slice(0..0)  # returns [99]
Shows slicing with negative indices and slicing beyond array size.
Ruby
letters = ['a', 'b', 'c', 'd']

# Slice from start to end
all_letters = letters[0..-1]  # gets all elements

# Slice last two elements
last_two = letters[-2..-1]

# Edge case: slice beyond array size
beyond = letters[2..10]  # returns ['c', 'd']
Sample Program
This program shows how to slice arrays using ranges in Ruby. It prints the original array, slices parts of it, and shows edge cases with empty and single element arrays.
Ruby
fruits = ['apple', 'banana', 'cherry', 'date', 'fig']

puts "Original array: #{fruits.join(' ')}"

# Slice first 3 fruits
first_three = fruits[0..2]
puts "First three fruits: #{first_three.join(' ')}"

# Slice middle fruits
middle_fruits = fruits[1...4]
puts "Middle fruits (index 1 to 3): #{middle_fruits.join(' ')}"

# Slice last two fruits
last_two = fruits[-2..-1]
puts "Last two fruits: #{last_two.join(' ')}"

# Edge case: slice empty array
empty_array = []
empty_slice = empty_array[0..2]
puts "Slice from empty array: #{empty_slice.join(' ')}"

# Edge case: slice single element array
single_array = ['kiwi']
single_slice = single_array[0..0]
puts "Slice single element array: #{single_slice.join(' ')}"
OutputSuccess
Important Notes
Time complexity of slicing is O(k), where k is the number of elements in the slice.
Space complexity is O(k) because a new array is created for the slice.
Common mistake: forgetting that ranges with two dots include the end index, while three dots exclude it.
Use slicing when you want a new array with part of the elements. Use other methods like 'select' if you want to filter by condition.
Summary
Array slicing lets you get parts of an array easily using ranges.
Ranges with '..' include the end index; '...' excludes it.
Slicing creates a new array and does not change the original.