|
| 1 | +# Accessing elements |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- how to get single elements from an Array |
| 7 | +- how to get subsets of elements from an Array |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +Each element in an Array is associated with a fixed place – its *index*. |
| 12 | +The index is an Integer which allows to access an element in the Array. |
| 13 | + |
| 14 | +The index 0 points to the first element, the index 1 to the second element, etc. |
| 15 | +Negative indexes point to elements backwards from the last element. E.g. -1 points |
| 16 | +to the last element, -2 points to the second last element, and so on. |
| 17 | + |
| 18 | +## Getting a single element from an Array |
| 19 | + |
| 20 | +You can get the object from an Array at an index by using squared brackets: |
| 21 | + |
| 22 | +` numbers = [2, 4, 9]` *# defining an Array `numbers`* |
| 23 | +` numbers[0]` *# => 2 |
| 24 | +` numbers[1]` *# => 4 |
| 25 | +` numbers[2]` *# => 9 |
| 26 | +` numbers[-1]` *# => 9 |
| 27 | + |
| 28 | +If there is no element present for the given index you will get a *nil* value: |
| 29 | + |
| 30 | +` numbers[3]` *# => nil |
| 31 | + |
| 32 | +`[]` is just an ordinary method that is defined for an instance of Array. |
| 33 | + |
| 34 | +Another way for getting a single element is using the alias method `at`: |
| 35 | + |
| 36 | +` numbers.at(0)` *# => 2 |
| 37 | +` numbers.at(-1)` *# => 9 |
| 38 | +` numbers.at(3)` *# => nil |
| 39 | + |
| 40 | +There are two methods for getting special elements of an Array: `first` and `last`. |
| 41 | +`first` – as you might have guessed – returns the first element (at index 0), |
| 42 | +and `last` returns the last element (at index -1): |
| 43 | + |
| 44 | +` numbers.first` *# => 2* |
| 45 | +` numbers.last` *# => 9* |
| 46 | + |
| 47 | +## Getting a subset of elements from an Array |
| 48 | + |
| 49 | +You can also pass 2 parameters to the `[]` method. This allows to select a subarray |
| 50 | +by defining the index to start at and the number of elements you want to grab, e.g.: |
| 51 | + |
| 52 | +` numbers[1, 2]` * # => [4, 9]* |
| 53 | + |
| 54 | +returns 2 elements starting at index 1. If there are no elements for the given indexes |
| 55 | +available, it returns *nil*: |
| 56 | + |
| 57 | +` numbers[10, 2]` * # => nil* |
| 58 | + |
| 59 | +Another way to select a subarray from an Array is to pass a *Range* of the wanted |
| 60 | +indexes to `[]`: |
| 61 | + |
| 62 | +` numbers[1..2]` * # => [4, 9] |
| 63 | + |
| 64 | +In case the elements are not available for the indexes it will again return *nil*: |
| 65 | + |
| 66 | +` numbers[5..10]` * # => nil* |
| 67 | + |
| 68 | +For further information about the `[]` method, see (ruby-doc core: Array#[]). |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +Create an Array of 5 different programming languages (as Strings) and store it in |
| 73 | +a *languages* variable. |
| 74 | + |
| 75 | +Define a variable *first* and assign the first programming language to it. |
| 76 | + |
| 77 | +Define a variable *last* and assign the last programming language to it. |
| 78 | + |
| 79 | +Store the middle 3 programming languages in a variable *some_languages*. |
| 80 | + |
| 81 | +--- |
0 commit comments