course n°3 · Découverte

Array, Set & Hash

Order, uniqueness, keys: three boxes, three reflexes. Your move — pick the right one.

~11 min7 stations
▶ video🧩 exercises📄 cheat sheet

42 seconds · English captions included · 8.3 Mo

The 7 things you'll know how to do

Lesson 3: the Containers

Regarde la leçon (42 s) : Array, Set, Hash — trois boîtes, trois réflexes, un seul bug quand tu te trompes.

Order above all

`fruits` doit contenir exactement 2 éléments : « pomme » à l'INDEX 0 et « poire » en dernier. L'index part de zéro, toujours.

Duplicates, out

Convertis `mots` (Array avec doublons) en **Set** : il doit contenir « ruby » et « ia » UNE seule fois chacun — soit size 2.

Memory with keys

Complète `infos` : clé String « langue » => « ruby » et clé « niveau » => 1 (l'ENTIER, pas une String). Puis lis `infos["langue"]`.

The triptych

Trois boîtes, trois réflexes : `file` = ["ann", "bob", "cat"] dans l'ordre · `uniques` = un Set de 1, 1, 2 (size 2) · `scores` = { "bob" => 20 }.

Boxes inside boxes

Compose : `groupe` est un Hash — clé « ruby » => un Array d'AU MOINS 2 prénoms dont « ann », clé « ia » => un Array d'au moins 1 prénom.

Lesson 3: OK

Sans aide : `inventaire` — un Hash avec clé « boites » => un Set d'AU MOINS 2 noms (chaînes), et clé « total » => le nombre exact de boîtes (pas un chiffre en dur si tu changes le Set).

Share the lesson

← all courses

📄 The cheat sheet

print it, stick it on your monitor, pretend it was your idea

📄 Cheat sheet — Array · Set · Hash

Three boxes, three reflexes. The wrong choice costs you bugs.

The reflex, once and for all

You have… You take Example
a sequence, an order that matters Array ["ann", "bob", "cat"]
unique values Set Set.new([1, 1, 2]) # => {1, 2}
names → values Hash {"bob" => 20}

Array — order, above all

fruits = ["pomme", "poire"]
fruits[0]    # => "pomme"   the index starts at ZERO. Always.
fruits.last  # => "poire"
fruits << "figue"          # pushes at the END (not the beginning!)

Trap #1: << appends at the end — if your value must be first, rewrite the array.

Set — duplicates, out

require "set"
mots = Set.new(["ruby", "ia", "ruby"])
mots.size          # => 2     "ruby" lives only once
mots.is_a?(Array)  # => false Set ≠ Array

Trap #1: mots.uniq gives you an Array without duplicates — not a Set. The lesson asks for a Set? That's Set.new.

Hash — memory with keys

infos = { "langue" => "ruby", "niveau" => 1 }
infos["langue"]   # => "ruby"
infos[:langue]    # => nil    :symbol and "string" are TWO different keys

Trap #1: :langue ≠ "langue". Drop the quotes, lose your value — nil, no warning. Trap #2: "1" ≠ 1. Your niveau == 1 depends on it.

Boxes inside boxes

groupe = { "ruby" => ["ann", "bob"], "ia" => ["cat"] }
groupe["ruby"][0]   # => "ann"

Nothing exotic: a Hash value can be anything — including another container.

The Ruby reflex

list  = []                    # order
uniq  = Set.new(list)         # unique (require "set")
table = {}                    # named

Never say again

  • "a hashed table" → a Hash (the table, that's the Array)
  • "the list without duplicates" → a Set — yes, it exists
  • "I found the key" → if it's nil, you found nothing at all 😉

SlopCourse Microduck — from slop to clean code.