📖 Scenario: You are helping a small bakery keep track of daily sales. You will use local variables with clear names to store and calculate the total number of bread loaves sold.
🎯 Goal: Create local variables with proper names to store daily sales numbers, add them up, and print the total loaves sold.
📋 What You'll Learn
Create local variables with lowercase letters and underscores
Use descriptive variable names for daily sales
Add the daily sales variables to get the total
Print the total number of loaves sold
💡 Why This Matters
🌍 Real World
Local variables are used everywhere in programming to store temporary information like daily sales, user input, or calculation results.
💼 Career
Clear variable naming and using local variables properly is a fundamental skill for any programmer, making your code easier to maintain and collaborate on.
Progress0 / 4 steps
1
Create local variables for daily sales
Create three local variables called monday_sales, tuesday_sales, and wednesday_sales with the values 20, 25, and 30 respectively.
Ruby
Hint
Use lowercase letters and underscores for variable names. Assign the exact numbers given.
2
Create a variable for total sales
Create a local variable called total_sales and set it to the sum of monday_sales, tuesday_sales, and wednesday_sales.
Ruby
Hint
Add the three daily sales variables and store the result in total_sales.
3
Print the total sales
Use puts to print the value of total_sales.
Ruby
Hint
Use puts followed by the variable name to display the total.
4
Run the program and see the output
Run the program to display the total number of bread loaves sold over the three days.
Ruby
Hint
The output should be the sum of 20, 25, and 30, which is 75.
Practice
(1/5)
1. Which of the following is a valid local variable name in Ruby?
easy
A. user_name
B. UserName
C. user-name
D. 1username
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 A
Quick Check:
Valid local variable = user_name [OK]
Hint: Local variables start lowercase or underscore, no hyphens or digits first [OK]
Common Mistakes:
Starting variable names with uppercase letters
Using hyphens instead of underscores
Starting variable names with numbers
2. Which of these Ruby variable names will cause a syntax error?
easy
A. temp-var
B. tempVar2
C. _tempVar
D. temp_var
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 A
Quick Check:
Hyphens cause syntax errors in variable names [OK]
Hint: No hyphens allowed in variable names, use underscores instead [OK]
Common Mistakes:
Using hyphens instead of underscores
Confusing uppercase and lowercase letters
Starting variable names with digits
3. What will be the output of this Ruby code?
def greet
name = "Alice"
Name = "Bob"
puts name
end
greet
medium
A. name
B. Alice
C. Bob
D. Name
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 B
Quick Check:
Local variable 'name' prints "Alice" [OK]
Hint: Local variables lowercase, constants uppercase; puts prints local variable [OK]
Common Mistakes:
Confusing constants with local variables
Assuming uppercase variable prints
Ignoring case sensitivity
4. Identify the error in this Ruby code snippet:
def calculate
total = 10
Total = 20
puts Total + total
end
calculate
medium
A. SyntaxError due to variable naming
B. Runtime error: undefined variable
C. No error, outputs 30
D. No error, outputs 10
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 C
Quick Check:
Constant + local variable sums correctly [OK]
Hint: Constants uppercase, locals lowercase; both can be used together [OK]
Common Mistakes:
Assuming case-insensitive variables
Expecting syntax error from uppercase variable
Confusing constants with local variables
5. You want to create a local variable to store a user's age in Ruby. Which of these variable names follows best naming conventions and is clear to other programmers?
hard
A. UserAge
B. userAge
C. user-age
D. user_age
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 D
Quick Check:
Use lowercase with underscores for clear local variables [OK]
Hint: Use lowercase and underscores for clear Ruby local variables [OK]
Common Mistakes:
Using camelCase instead of snake_case
Starting variable names with uppercase letters
Using hyphens in variable names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.