array ruby taking 3 last value

Ruby: Extracting the Last Three Values from an Array

# Given array
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Extracting the last three values
result = arr.last(3)
  1. Initialize the Array:
  2. Initialize the array with some values.
  3. arr = [1, 2, 3, 4, 5, 6, 7, 8, 9].

  4. Extracting the Last Three Values:

  5. Use the last method with an argument of 3 to extract the last three values from the array.
  6. result = arr.last(3).

  7. Explanation:

  8. The last method in Ruby returns the last element(s) from the array.
  9. When called with an argument of 3, it returns an array containing the last three elements of the original array.
  10. This method is useful for extracting a specific number of elements from the end of an array.