Objects
An object is a box. And you already made one this morning.
42 seconds · English captions included · 8.6 Mo
Watched it? Now play it.
7 stations, real Ruby running in your browser, zero red FAILED. ClaudiaSI is waiting.
The 7 things you'll know how to do
Lesson 1: the Objects
Regarde la leçon (42 s), c'est le contrat : un objet, c'est une BOÎTE qui SAIT et qui FAIT des trucs.
Create your first box
Crée un objet : complète `boite = Boite.new`. Un .new = une boîte qui apparaît.
Draw the mold
Définis la classe : `class ChatBot` … `end`. La classe, c'est le moule, pas la boîte.
The click
Passe à la presse : `bot = ChatBot.new` puis `bot.hello` qui renvoie « Salut, je suis un objet ».
Knows vs does
Donne-lui un état ET une méthode qui l'utilise : `bot = ChatBot.new('Ruby')` et `bot.pseudo` qui renvoie « Ruby ».
Two boxes, two memories
Crée `bot1 = ChatBot.new('un')` et `bot2 = ChatBot.new('deux')` — puis vérifie qu'ils ne partagent PAS leur mémoire.
Lesson 1: OK
Sans aide : écris une classe avec une méthode `salut` qui renvoie « Salut, je suis un objet », et une instance `moi`.
📄 The cheat sheet
print it, stick it on your monitor, pretend it was your idea📄 Cheat sheet — Objects (Ruby)
One page, five lines, and you can talk about objects without bluffing.
The reflex
class ChatBot # 1. the class = the MOLD
def hello # 2. the method = what it KNOWS HOW TO DO
"Salut, je suis un objet"
end
end
bot = ChatBot.new # 3. .new = the CLICK → an object is born
bot.hello # 4. talk to the object → "Salut, je suis un objet"
The 4 truths
| What you see | What it really is |
|---|---|
class ChatBot |
the mold — it exists only once |
ChatBot.new |
a box built from the mold |
@pseudo |
its state — what it KNOWS |
def pseudo |
its method — what it DOES |
The sentence that unlocks everything
An object is a box: it KNOWS things and it DOES things.
Trap #1 (everyone's)
bot1 = ChatBot.new("un")
bot2 = ChatBot.new("deux")
bot1.pseudo # => "un"
bot2.pseudo # => "deux" # bot1 has NO IDEA what bot2 lives
Two .new = two objects = two memories. Always. Even if they're twins.
Never say again
"an obj variable"→ an instance"the class does…"→ an instance does…, the class is the mold"copy the object"→ in your head maybe; in Ruby,.dupif you insist
SlopCourse — from slop to clean code.