| Start | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Create Array of Strings Arrays of strings can be created using ruby's percent string (opens new window) syntax: This is functionally equivalent to defining the array as: Instead of It is also possible to use arbitrary non-alphanumeric delimiters, such as:
Multiple words can be interpreted by escaping the space with a . |
||||||||||||||||||||||||||||||
| Create Array with Array::new An empty Array ( To set the length of the array, pass a numerical argument: There are two ways to populate an array with default values:
|
||||||||||||||||||||||||||||||
Create Array of Symbols Creates the array Instead of Additionally, if you want to use interpolation, you can do this with Creates the arrays:
|
||||||||||||||||||||||||||||||
| Manipulating Array Elements Adding elements: Removing elements: Combining arrays: You can also multiply arrays, e.g. |
||||||||||||||||||||||||||||||
| Creating an Array with the literal constructor [ ] Arrays can be created by enclosing a list of elements in square brackets ( Arrays can contain any kind of objects in any combination with no restrictions on type: |
||||||||||||||||||||||||||||||
| Accessing elements You can access the elements of an array by their indices.
Array index numbering starts at You can crop an array using range This returns a new array, but doesn't affect the original. Ruby also supports the use of negative indices. You can combine negative and positive indices as well Other useful methods Use Or Similarly for Use Or |
||||||||||||||||||||||||||||||
Arrays union, intersection and difference Union ( Intersection ( Difference ( |
||||||||||||||||||||||||||||||
| Decomposition Any array can be quickly decomposed by assigning its elements into multiple variables. A simple example: Preceding a variable with the splat operator ( Decomposition is safe and never raises errors. Decomposition tries to call If the object being decomposed doesn't Decomposition can also be nested by using a This is effectively the opposite of splat. Actually, any decomposition expression can be delimited by Edge case: a single identifier cannot be used as a destructuring pattern, be it outer or a nested one: When assigning an array literal to a destructuring expression, outer This is known as parallel assignment, but it uses the same decomposition under the hood. This is particularly handy for exchanging variables' values without employing additional temporary variables: Values are captured when building the right-hand side of the assignment, so using the same variables as source and destination is relatively safe. |
||||||||||||||||||||||||||||||
| #map
The original array is not modified; a new array is returned containing the transformed values in the same order as the source values. In
|
||||||||||||||||||||||||||||||
| Filtering arrays Often we want to operate only on elements of an array that fulfill a specific condition: Select (opens new window)Will return elements that match a specific condition Reject (opens new window)Will return elements that do not match a specific condition Both |
||||||||||||||||||||||||||||||
| Inject, reduce Inject and reduce are different names for the same thing. In other languages these functions are often called folds (like foldl or foldr). These methods are available on every Enumerable object. Inject takes a two argument function and applies that to all of the pairs of elements in the Array. For the array Here we pass the function a starting value and a block that says to add all of the values together. The block is first run with If we omit the first argument, it will set In addition, instead of passing a block with a function, we can pass a named function as a symbol, either with a starting value, or without. With this, the above example could be written as: or omitting the starting value: |
||||||||||||||||||||||||||||||
| Two-dimensional array Using the For instance, to create a 3x4 array of zeros: The array generated above looks like this when printed with You can read or write to elements like this: |
||||||||||||||||||||||||||||||
| Arrays and the splat (*) operator The This can be used to wrap a single object in an Array if it is not already: In the above example, the If If If The splat operator is particularly handy when used as an argument in methods in some cases. For example, it allows |
||||||||||||||||||||||||||||||
Turn multi-dimensional array into a one-dimensional (flattened) array If you have a multi-dimensional array and you need to make it a simple (i.e. one-dimensional) array, you can use the |
||||||||||||||||||||||||||||||
| Get unique array elements In case you need to read an array elements avoiding repetitions you case use the Instead, if you want to remove all duplicated elements from an array, you may use While the output is the same, |
||||||||||||||||||||||||||||||
| Get all combinations / permutations of an array The If this method is called without a block, it will return an
The
Calling the combination method by itself will result in an enumerator. To get an array, call the The For example the sequences
|
||||||||||||||||||||||||||||||
| Remove all nil elements from an array with #compact If an array happens to have one or more Finally, notice that if |
||||||||||||||||||||||||||||||
| Create an Array of consecutive numbers or letters This can be easily accomplished by calling
or A convenient shortcut for creating an array is |
||||||||||||||||||||||||||||||
| Create Array of numbers The normal way to create an array of numbers: Range objects can be used extensively to create an array of numbers:
All the above methods load the numbers eagerly. If you have to load them lazily: |
||||||||||||||||||||||||||||||
| Cast to Array from any object To get Array from any object, use The following is an example: For example, you could replace to the following code. Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference