What a variable actually is

Free preview 3 min to read

Your first real Ruby, finally

The last course was all mindset, with no code to run. This one changes that. Variables are the first concrete Ruby syntax in this series, and everything after builds on them.

A name standing in for a value

A variable is a name you choose, linked to a value, so you can use that value again without retyping or recomputing it.

price = 29.99
puts price
puts price * 2

price means nothing by itself. It's a label you chose, pointing at 29.99. Every later use of price refers to that value until something reassigns it. That simple mechanism is what every Ruby program is built from.

= Is assignment, not a claim of equality

This character causes more early confusion than anything else in this course. = assigns a value to a name. It doesn't claim two things are equal, even though it looks like math class.

age = 25

Read it as an instruction: "store the value 25 under the name age." It isn't the claim "age equals 25". That matters once you meet ==, the real equality comparison, in lesson 3. The two look almost the same and mean different things.

No type to declare, no keyword required

Some languages make you announce a variable's type or use a keyword first, like int age = 25 or let age = 25. Ruby, true to the last course's theme, skips that ceremony:

age = 25          # just works, no "int" needed
name = "Ada"      # just works, no "string" needed
is_admin = true   # just works, no "boolean" needed

No keyword creates the variable, and no type is declared. The first time Ruby sees age = 25, age starts existing, holding whatever is on the right of the =. A later course covers the kinds of values a variable can hold. For now, notice that nothing stands between you and a working variable.

Reassignment: the same name, a new value

Nothing locks a variable to its first value. Assign the name again and it points at the new value.

age = 25
puts age   # => 25

age = 26
puts age   # => 26

That's what "variable" means: a name whose value can change. You'll use this constantly, for a running total, a counter or a status that moves forward.

What this course covers

  • Lesson 1, What a variable is: a name standing in for a value, and why = isn't equality (you are here)
  • Lesson 2, Local variables, naming and snake_case: Ruby's naming rules and community convention
  • Lesson 3, Assignment is not equality: = vs ==, with real code
  • Lesson 4, Local scope: where a variable lives, and where it stops existing
  • Lesson 5, Instance variables (@): a kind of variable that belongs to an object
  • Lesson 6, nil and undefined variables: what happens when you use something never set
  • Lesson 7, Putting it together: a small challenge combining the whole course

By the end, you'll write and reason about variables the way a working Ruby developer does.

Lesson 1 of 7 · free

Keep going with lesson 2

Local variables: naming and snake case

Local variable names start with a lowercase letter or underscore, and Ruby's convention is descriptive snake_case.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...