Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 3 von 12

Everything is an object

Numbers have methods, strings have methods, even nil has methods — Ruby's single organizing idea.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Every language has a worldview. Ruby's fits in one sentence: everything is an object, and you talk to objects by sending them messages (methods). Absorb this and the whole language unfolds logically.

Numbers are objects

RubyCloud-Run

In most languages, these would be functions applied to numbers. In Ruby the number itself knows: "7, are you even?" — you ask the object. The ? suffix is a lovely convention: methods ending in ? answer true/false.

Strings are objects (of course)

RubyCloud-Run

Discovering what an object can do

Every object answers .class (what am I?) and .methods (what can I do?):

RubyCloud-Run

Even nil — Ruby's "nothing" — is a real object with a class (NilClass) and methods (nil.to_s gives ""). No special cases, no exceptions to memorize: it's objects all the way down.

Checkpoint

What does 10.even? return?

Checkpoint

Ruby methods ending in ? conventionally…