Limited Offer
60% OFF on all plans!

Arrays in Ruby

Create and manipulate ordered collections using Ruby arrays, including common array methods and iteration.

Array Creation

Arrays are created with `[]`, `Array.new`, or shorthand notations like `%w` for string arrays and `%i` for symbol arrays.

1. Array Literals

The most common way to create an array is with square brackets.

2. Array.new

`Array.new(size, default)` creates an array with a preset size and value.

3. %w and %i Shorthand

`%w[...]` creates a string array; `%i[...]` creates a symbol array — no quotes or commas needed.

Array Fundamentals

Arrays are zero-indexed. Ruby supports negative indices, ranges for slicing, and `first`/`last` shortcuts.

1. Indexing & Slicing

Access elements by index. Negative indices count from the end.

2. Length & Empty Check

`length`/`size` return the element count; `empty?` checks if there are none.

3. Nested Arrays & flatten

Arrays can contain other arrays. `flatten` collapses them into one level.

Traversing an Array

Use `each`, `each_with_index`, `each_with_object`, and `map` to traverse arrays.

1. each

Iterate over every element with `each`.

2. each_with_index

Get the element and its index simultaneously.

Searching, Joining & Comparing

Ruby arrays support `include?`, `find`, `index`, `join`, and set-like operators for comparison.

1. include? and find

`include?` checks membership; `find` returns the first matching element.

2. index and rindex

`index` finds the position of an element; `rindex` finds the last occurrence.

3. join

`join` converts an array to a string with an optional separator.

4. Comparing Arrays

Use `==` for equality and set operators `|`, `&`, `-` for set operations.

Insertion & Removal

Ruby arrays are mutable. Add elements with `push`/`<<`/`unshift`, remove with `pop`/`shift`/`delete`/`compact`.

1. push, pop, <<

Add/remove from the end with `push`/`pop`; the `<<` shovel appends.

2. unshift and shift

`unshift` adds to the front; `shift` removes from the front (queue/FIFO pattern).

3. delete, delete_at, and compact

`delete` removes by value; `delete_at` removes by index; `compact` removes nils.

Advanced Array Operations

Ruby arrays support rich operations: sorting, flattening nested arrays, zipping, rotating, sampling, and combining.

1. Sorting

`sort` returns a sorted array; `sort_by` sorts by a computed value.

2. uniq, flatten, compact

Remove duplicates, flatten nested arrays, or strip nils.

3. zip and product

`zip` pairs elements from two arrays; `product` gives all combinations.

4. sample and shuffle

`sample` picks a random element; `shuffle` randomizes order.

Higher-Order Functions (Enumerable)

Ruby's `Enumerable` module provides powerful higher-order methods: `map`, `select`, `reject`, `reduce`, `any?`, `all?`, `none?`, `flat_map`, and more.

1. map — Transform Every Element

`map` returns a new array with each element transformed by the block.

2. select and reject — Filter

`select` keeps elements where the block is true; `reject` keeps where false.

3. reduce — Accumulate

`reduce` (alias: `inject`) combines all elements into a single value.

4. any?, all?, none?

Test whether some, all, or no elements satisfy a condition.

5. flat_map

`flat_map` maps and then flattens one level — equivalent to `map(...).flatten(1)`.