Here are a few quick examples :
First, initialize a new array. Add some elements to it (1,2,3). Remove an element (1). If you add multiple copies of 1, then all instances of 1 are deleted if you do arr0 -= 
Other ways in which new arrays can be defined. Much of this is syntactical sugar.
Sorting an array with a comparator function of our choice. For this we use the sort_by method offered by enumerable.
Let's say, we want to sort an array of integers, in ascending order of f(x) where f(x) = x^2 - x^3 + x + 20.
Suppose we have an array of integers. And we want to select only those which are even. We can use the "select" method over here. Select, transforms a given array into one which only contains the elements which satisfy a certain condition (or predicate).
Or you can use select! which modifies the original array and leaves only the odd (or even) elements in it.
Applying a function to each element of a list. This can be done using the map method.
Suppose, we want a new array, such that, for every element, x in the original array, the new array contains 2x+10.
Here's how we could go about this :
Here's how you could initialize multi-dimensional array in Ruby. The multi-dimensional array, as initialized below, has 10 rows and 5 columns, all initialized to zero.
Or you could use 'matrix' which is a package bundled with Ruby.
Check out some of our other Ruby Tutorials :
Computer Science >