Skip to content
KodeTrail

Camp 1 · Step 3 of 12

Everything is an object

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

12 min+50 XP

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…