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
<<andpushboth append;firstandlastpeek at the ends.- Ranges slice:
langs[1..2]returns a new two-element array. sort,uniqandsumcover the daily transformations.
Keywords and builtins used here
puts
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.
Step 1 of 3 in Collections, step 4 of 29 in Language basics.