Local variables store temporary information inside a small part of your program. Naming them clearly helps you and others understand what the program does.
Local variables and naming conventions in Ruby
variable_name = value
Local variable names start with a lowercase letter or underscore.
Use only letters, numbers, and underscores; no spaces or special characters.
count holding the number 10.count = 10_temp = "hello"user_name = "Alice"This program uses local variables name and greeting_message inside the greet method to store and print a greeting.
def greet name = "Bob" greeting_message = "Hello, #{name}!" puts greeting_message end greet
Local variables only exist inside the method or block where they are created.
Choose clear and descriptive names to make your code easier to understand.
Do not start local variable names with capital letters; those are for constants or classes.
Local variables hold temporary data inside methods or blocks.
Names start with lowercase letters or underscores and use only letters, numbers, and underscores.
Good naming helps keep code clear and easy to read.
Practice
Solution
Step 1: Check the first character of each variable name
Local variables must start with a lowercase letter or underscore. 'user_name' starts with 'u' which is lowercase.Step 2: Check for invalid characters
Variable names cannot contain hyphens or start with numbers. 'user-name' has a hyphen and '1username' starts with a number, so they are invalid.Final Answer:
user_name -> Option AQuick Check:
Valid local variable = user_name [OK]
- Starting variable names with uppercase letters
- Using hyphens instead of underscores
- Starting variable names with numbers
Solution
Step 1: Identify invalid characters in variable names
Ruby variable names can only contain letters, digits, and underscores. Hyphens are not allowed.Step 2: Check each option for invalid characters
temp-var has a hyphen, which causes a syntax error. The others are valid.Final Answer:
temp-var -> Option AQuick Check:
Hyphens cause syntax errors in variable names [OK]
- Using hyphens instead of underscores
- Confusing uppercase and lowercase letters
- Starting variable names with digits
def greet name = "Alice" Name = "Bob" puts name end greet
Solution
Step 1: Understand variable naming and scope
In Ruby, local variables start with lowercase letters, constants start with uppercase. 'name' is local variable, 'Name' is a constant.Step 2: Check which variable is printed
The code prints 'name', which is "Alice". The constant 'Name' is not used in the puts statement.Final Answer:
Alice -> Option BQuick Check:
Local variable 'name' prints "Alice" [OK]
- Confusing constants with local variables
- Assuming uppercase variable prints
- Ignoring case sensitivity
def calculate total = 10 Total = 20 puts Total + total end calculate
Solution
Step 1: Analyze variable types and naming
'total' is a local variable, 'Total' is a constant. Both are defined before use.Step 2: Evaluate the expression in puts
Adding 'Total' (20) and 'total' (10) results in 30. No syntax or runtime errors occur.Final Answer:
No error, outputs 30 -> Option CQuick Check:
Constant + local variable sums correctly [OK]
- Assuming case-insensitive variables
- Expecting syntax error from uppercase variable
- Confusing constants with local variables
Solution
Step 1: Check Ruby naming conventions for local variables
Local variables should start with lowercase letters or underscores and use underscores to separate words for clarity.Step 2: Evaluate each option for clarity and convention
'user_age' uses lowercase and underscores, making it clear and conventional. 'UserAge' starts uppercase (looks like constant), 'user-age' has invalid hyphen, 'userAge' uses camelCase which is less common in Ruby.Final Answer:
user_age -> Option DQuick Check:
Use lowercase with underscores for clear local variables [OK]
- Using camelCase instead of snake_case
- Starting variable names with uppercase letters
- Using hyphens in variable names
Ready to prove it?
Pass the test and this pattern moves unknown
