Variables
Assignment
A variable stores a value under a name so you can reuse it later. The single equals sign stores the object on the right under the name on the left.
name = "Ada" age = 36 puts name # prints Ada puts age # prints 36
Variables hold any object, and you may store a new value in the same name at any time. Ruby never asks you to declare a type first.
Naming rules snake_case
Local variable names start with a lowercase letter or an underscore, then use lowercase letters, digits, and underscores. Words are joined with underscores, a style called snake_case.
Names cannot start with a digit and cannot contain spaces or dashes. A leading capital letter would turn the name into a constant, which is the topic of the last section.
first_name = "Ada" score_2 = 10 puts first_name # prints Ada puts score_2 # prints 10
Parallel assignment
Ruby can fill several variables in one line when the counts on both sides match. The first value goes into the first name, the second into the second name, and so on.
x, y = 1, 2 puts x # prints 1 puts y # prints 2
The classic trick is a swap with no helper variable. The right side builds a pair first, then Ruby unpacks it into the two names:
x, y = y, x puts x # prints 2 puts y # prints 1
Constants shout
A constant holds a value that should stay fixed, such as a limit or a title. Constants start with a capital letter, and multi-word constants use all caps with underscores.
LIMIT = 10 APP_NAME = "Demo" puts LIMIT # prints 10
Ruby only warns when you reassign a constant instead of stopping you, so treat the warning as an error in your own code and pick a new name when a value must change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
name = "Ada" age = 36 puts nameAnswer
Ada
The name holds the string, and puts prints it without the quotes.
-
Which name is a valid local variable in Ruby?
Answer
first_name
A local name starts lowercase or with an underscore and holds only letters, digits and underscores.
-
What does a leading capital letter in a name make it?
Answer
A constant
Ruby reads
Limitas a constant; only a lowercase or underscore first letter starts a local name. -
What does this program print?
x, y = 1, 2 puts yAnswer
2
The pair is unpacked in order, so y gets the second value.
-
What does this program print?
x, y = 1, 2 x, y = y, x puts x puts yAnswer
2 / 1
The right side is built as the pair (2, 1) before anything is assigned, which swaps the values.
-
What does a parallel assignment need to fill two names with two values?
Answer
Both sides on one line, separated by commas
x, y = 1, 2pairs them in order; no declaration is needed. -
Which name is a constant by Ruby's rule?
Answer
APP_NAME
A constant starts with a capital letter; all caps with underscores is the convention for several words.
-
What happens when you assign a new value to a constant at the top level?
Answer
Ruby prints a warning and keeps the new value
Ruby says "already initialized constant" and the new value wins, which is why the lesson calls the warning an error.
-
What does this program print?
LIMIT = 10 APP_NAME = "Demo" puts "#{APP_NAME} allows #{LIMIT}"Answer
Demo allows 10
Interpolation reads the value of both constants into the string.
-
Which of these names does Ruby accept as a local variable?
Answer
total_sum
A space, a dash and a capital first letter are all rejected for a local name.
Article author: Arthur Isaev