Intern Track Roadmap

Ruby collections: arrays

Ruby arrays hold an ordered list of values behind one name. This course covers creating arrays with square brackets, reading elements by zero-based and negative indices, and using first, last, length and size. The methods push, <<, pop, unshift and shift change an array in place. The methods include?, index and empty? answer common questions, and slicing works with [start, length] or a range. It also covers sort, reverse and their bang versions, cleaning with uniq, compact and flatten, and combining arrays with +, -, & and |.

What you'll learn

  • ✓Creating arrays and reading elements with zero-based and negative indices
  • ✓Adding and removing elements at both ends with push, <<, pop, unshift and shift
  • ✓Searching arrays with include?, index and empty?
  • ✓Slicing with [start, length], ranges, first(n) and last(n)
  • ✓Sorting and reversing, and the difference between sort and sort!
  • ✓Cleaning and combining arrays with uniq, compact, flatten, +, -, & and |
Free preview · Lesson 1 3 min read

Creating and accessing arrays

Every value so far, a number, a string or a boolean, lived alone in its own variable. An array holds an ordered list of values behind one name.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...