# HackerRank Ruby Array Index Part 1 Solution

Hello coders, In this post, you will learn how to solve HackerRank Ruby Array Index Part 1 Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

## HackerRank Ruby Array Index Part 1 Solution

Let’s get started with Ruby Array Index Part 1 Solution

### Problem Statement

Array collections offer various ways to access their elements.

The positions are `0` indexed. Objects of the array can be accessed using the `[]` method which may take various arguments, as explained below.

```arr = [9, 5, 1, 2, 3, 4, 0, -1]
```
• A number which is the position of element
```>>arr[4]
=> 3```

or

```>>arr.at(4)
=> 3 ```
• A range indicating the start and the end position
```>>arr[1..3] # .. indicates both indices are inclusive.
=> [5,1,2]
>>arr[1...3] # ... indicates the last index is excluded.
=> [5,1]```
• Start index and the length of the range
```>>arr[1,4]
=> [5, 1, 2, 3]```

For this challenge, your task is to complete the functions using syntax as explained above.

### HackerRank Ruby Array Index Part 1 Solution

```def element_at(arr, index)
# return the element of the Array variable `arr` at the position `index`
# arr.at(index) # or
return arr[index]
end
def inclusive_range(arr, start_pos, end_pos)
# return the elements of the Array variable `arr` between the start_pos and end_pos (both inclusive)
return arr[start_pos..end_pos]
end
def non_inclusive_range(arr, start_pos, end_pos)
# return the elements of the Array variable `arr`, start_pos inclusive and end_pos exclusive
return arr[start_pos...end_pos]
end
def start_and_length(arr, start_pos, length)
# return `length` elements of the Array variable `arr` starting from `start_pos`
return arr[start_pos, length]
end```

Note: This problem (Ruby Array – Index, Part 1) is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring