Ruby: Collections: Free Ruby on Rails Tutorial
Master Ruby Collections with Interactive Hands-On Practice
Ruby Collections Mastery Path
Arrays
Start with the simplest collections using square brackets. Learn indexing, appending, and element replacement with hands-on Terminal practice.
Hashes
Master key-value pairs with symbols and strings. Compare legacy and modern syntax while building practical examples like town councils.
Iterators
Transform collections with map, find, and reject methods. Chain operations to filter and manipulate data efficiently.
This tutorial requires active participation using IRB (Interactive Ruby) in Terminal. You'll be typing commands and seeing immediate results throughout each section.
Launch Interactive Ruby Environment
Open Terminal
Access your system's command line interface to begin working with Ruby interactively
Initialize IRB
Type 'irb' to start Interactive Ruby and begin executing Ruby commands in real-time
Verify Setup
You should see the IRB prompt ready to accept Ruby commands and provide immediate feedback
Array Operations Comparison
| Feature | Operation | Syntax | Result |
|---|---|---|---|
| Create Empty | bird_types = [] | [] | |
| Create with Values | ["Robin", "Finch", "Dove"] | Populated array | |
| Append Item | bird_types << "Woodpecker" | Adds to end | |
| Access Element | bird_types[0] | Returns "Robin" |
Ruby arrays start counting from zero, so the first element is at position [0], second at [1], and so on. This matches substring access patterns you've learned previously.
Hash Syntax Comparison
| Feature | Legacy Syntax | Modern Syntax |
|---|---|---|
| Definition | :president => "Marcus Aurelius" | president: "Marcus Aurelius" |
| Readability | Harder to read | Cleaner and shorter |
| Usage | Found in older code | Preferred for new projects |
Hash Key-Value Structure
Keys
Symbols like :president that are internal to your application. They act as identifiers for accessing specific data.
Values
The actual information like "Marcus Aurelius" that will be visible to end users and contains the meaningful data.
Working with Enumerators
Create Enumerator
Use .each method on collection to create an enumerator object that can iterate over elements
Access Next Element
Call .next method repeatedly to step through each element in sequence, starting with the first
Multiple Collections
Enumerators become powerful when stepping through multiple collections simultaneously in complex operations
Iterator Method Comparison
| Feature | Method | Purpose | Returns |
|---|---|---|---|
| map | Transform each element | New array with transformations | |
| reject | Filter out unwanted elements | Array without rejected items | |
| find | Get first matching element | Single element or nil | |
| find_all | Get all matching elements | Array of all matches |
You can chain iterator methods together with periods, like using map to transform data then reject to filter results, creating powerful data processing pipelines.
beatle[1] calls the item at position 1 of the hash, which will be the name of each Beatle
Key Takeaways