9.2. Collection handling with arrays

Arrays are the bread-and-butter way to handle collections of objects. We’ll put arrays through their paces in this section: we’ll look at the varied techniques available for creating arrays; how to insert, retrieve, and remove array elements; combining arrays with each other; transforming arrays (for example, flattening a nested array into a one-dimensional array); and querying arrays as to their properties and state.

9.2.1. Creating a new array

You can create an array in one of four ways:

  • With the Array.new method
  • With the literal array constructor (square brackets)
  • With a top-level method called Array
  • With the special %w{...} and %i{...} notations

You’ll see all of these techniques in heavy rotation in ...

Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.