Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Array.slice / [] / first / last

Array.slice / [] / first / last

Methods for extracting a portion of an array. You can use slice and [] to retrieve elements by index or range, first and last to get elements from the beginning or end, and take and drop to extract or skip a specified number of elements.

Syntax

# Retrieve elements by index or range.
array[index]
array[start, length]
array[range]
array.slice(index)
array.slice(start, length)

# Retrieve elements from the beginning or end.
array.first        # First element
array.first(n)     # First n elements (returns an array)
array.last         # Last element
array.last(n)      # Last n elements (returns an array)

# Retrieve or skip elements while a condition holds.
array.take(n)
array.take_while { |element| condition }
array.drop(n)
array.drop_while { |element| condition }

Method List

MethodDescription
slice(index)Returns the element at the specified index. A negative index counts from the end of the array.
slice(start, length)Returns a subarray starting at start with the specified number of elements.
slice(range)Returns the elements specified by a range object as an array.
first / first(n)Returns the first element, or the first n elements as an array.
last / last(n)Returns the last element, or the last n elements as an array.
take(n)Returns the first n elements as an array.
take_while { |e| ... }Returns elements as long as the block returns true.
drop(n)Returns the remaining array after skipping the first n elements.
drop_while { |e| ... }Skips elements as long as the block returns true.

Sample Code

arr = [10, 20, 30, 40, 50, 60, 70]

# Retrieve by index
puts arr[0]         # 10 (first element)
puts arr[-1]        # 70 (last element)
puts arr[2, 3].inspect  # [30, 40, 50] (3 elements starting at index 2)
puts arr[1..3].inspect  # [20, 30, 40] (specified by range object)
puts arr[1...3].inspect # [20, 30] (exclusive range, end not included)

# first / last
puts arr.first      # 10
puts arr.first(3).inspect  # [10, 20, 30]
puts arr.last       # 70
puts arr.last(2).inspect   # [60, 70]

# take / drop
puts arr.take(3).inspect   # [10, 20, 30]
puts arr.drop(4).inspect   # [50, 60, 70]

# Conditional retrieval and skipping
numbers = [2, 4, 6, 7, 8, 10]
puts numbers.take_while { |n| n.even? }.inspect   # [2, 4, 6] (stops at first odd number)
puts numbers.drop_while { |n| n.even? }.inspect   # [7, 8, 10] (starts from first odd number)

# Pagination example
items = (1..100).to_a
page = 2
per_page = 10
result = items.drop((page - 1) * per_page).take(per_page)
puts result.inspect   # [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] (page 2)

Notes

[] and slice behave almost identically, but differ slightly when an out-of-bounds index is specified. If you specify an index beyond the length of the array, nil is returned, so it is recommended to check for nil before using the return value.

When called without an argument, first and last return the element itself (not an array). When called with an argument, they always return an array. Be aware of this difference — for example, first(1) returns a one-element array.

Using take and drop together makes it easy to split an array. You can intuitively express operations like "skip N items and take M items", as used in pagination. The arr[1..3] syntax combined with a range object is also very commonly used.

If you find any errors or copyright issues, please .