|
| 1 | +# Creating Arrays |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- what an Array is |
| 7 | +- what you can store in an Array |
| 8 | +- how to create Arrays |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +An Array is a *collection of objects*. |
| 13 | + |
| 14 | +This collection is *ordered*, which means that each object in an Array has a fixed |
| 15 | +place assigned, a so called *index*. |
| 16 | +The index is defined by an integer starting with 0: The first object in the Array |
| 17 | +has the index 0, the second has the index 1, etc. |
| 18 | + |
| 19 | +The index can be used to access the objects in an Array. You will learn more |
| 20 | +about using the index in the next unit "Accessing elements". |
| 21 | + |
| 22 | +An Array can hold all kinds of Ruby objects, such as Strings, Numbers, Booleans, |
| 23 | +other Arrays, etc. The objects in an Array don‘t need to be of the same type: |
| 24 | +An Array can hold all kinds of different objects (e.g. Strings, and Numbers) at the same time. |
| 25 | + |
| 26 | +Let‘s create some Arrays! |
| 27 | + |
| 28 | +## Creating an Array with [] |
| 29 | + |
| 30 | +Arrays can be created in different ways. |
| 31 | +The shortest is surrounding a comma-separated list of objects with square brackets: |
| 32 | + |
| 33 | +` [1, 'Mary', true]` *# => [1, "Mary", true]* |
| 34 | + |
| 35 | +An Array is a Ruby object itself. If you ask for an Array‘s class with the `class` method: |
| 36 | + |
| 37 | +` numbers = [1, 2, 3]` |
| 38 | +` numbers.class` *# => Array* |
| 39 | + |
| 40 | +you will get `Array`. An Array is an object of type *Array*. |
| 41 | + |
| 42 | +Instead of using `[]` you can also use the the longer way and call the `[]` method |
| 43 | +of the *Array* class: |
| 44 | + |
| 45 | +` Array[1, 2, 3]` *# => [1, 2, 3]* |
| 46 | + |
| 47 | +## Creating an Array with #new |
| 48 | + |
| 49 | +Besides using `[]`, you can create an Array by instanciating an Array with the `new` method. |
| 50 | +The `new` method accepts 2 optional parameters: The number of elements, and the element itself. |
| 51 | + |
| 52 | +If you call `new` without any parameter it will create an empty Array: |
| 53 | + |
| 54 | +` Array.new` * # => []* |
| 55 | + |
| 56 | +If you only define the first parameter as an Integer, it will use this Integer to define |
| 57 | +the length of the new Array and will assign *nil* for each element: |
| 58 | + |
| 59 | +` Array.new(2)` * # => [nil, nil] |
| 60 | + |
| 61 | +If you pass an Array as first parameter it will return the defined Array: |
| 62 | + |
| 63 | +` Array.new([1, 2, 3])` * # => [1, 2, 3]* |
| 64 | + |
| 65 | +The actual interesting case is, if you pass both parameters. |
| 66 | +It will create an array of the length given in the first parameter, with each element |
| 67 | +being the second parameter. Here is an example: |
| 68 | + |
| 69 | +` Array.new(3, 4.5)` * # => [4.5, 4.5, 4.5]* |
| 70 | +` Array.new(2, ['a', 'b'])` * # => [['a', 'b'], ['a', 'b']] |
| 71 | + |
| 72 | + |
| 73 | +Another way of creating an Array is calling `Array()` with 1 parameter: |
| 74 | + |
| 75 | +` Array('banana')` * # => ["banana"]* |
| 76 | + |
| 77 | +This will create a new Array with only the given value and is the same as calling `['banana']`. |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +Assign an Array to a variable *objects* by using the square brackets syntax for creating the Array. |
| 82 | +The Array should have 3 elements: a Float, a Boolean, and an empty Array. |
| 83 | + |
| 84 | +Create an Array that holds 3 times the String *'?'* by using the `new` method. |
| 85 | +Assign the Array to a variable *question_marks*. |
| 86 | + |
| 87 | +--- |
0 commit comments