Codectionary / Developer documentation / Ruby

Ruby Arrays & Hashes

Arrays are ordered collections written with square brackets. Hashes store key-value pairs and are commonly written with symbol keys such as :name.

Syntax

tags = ["ruby", "web"]
profile = { name: "Ada", active: true }

Examples

Reading collection values

A small, runnable example of this syntax.

tags = ["ruby", "web"]
profile = { name: "Ada", active: true }

puts tags.first
puts profile[:name]

Best practices

  • Use symbols for stable, internal hash keys; use strings when a key naturally comes from external text or JSON.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Expressive scripting and applications
File extension
.rb
Runs in
Ruby interpreter
Usually used with
Ruby standard library and gems

Specifications & further reading

Related Ruby documentation