typestar

Arrays in Ruby

Building, growing and querying Ruby's workhorse collection.

langs = ["ruby", "python", "go"]
langs << "rust"
langs.push("c")
puts langs.length
puts langs.first
puts langs.last
puts langs[1..2].inspect

nums = [3, 1, 4, 1, 5, 9, 2, 6]
puts nums.sort.inspect
puts nums.uniq.inspect
puts nums.sum

How it works

  1. << and push both append; first and last peek at the ends.
  2. Ranges slice: langs[1..2] returns a new two-element array.
  3. sort, uniq and sum cover the daily transformations.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
233
Tokens
84
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 56 seconds.

Type this snippet

Step 1 of 3 in Collections, step 4 of 29 in Language basics.

← Previous Next →

Arrays in other languages