0
0
Rubyprogramming~5 mins

Local variables and naming conventions in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a local variable in Ruby?
A local variable is a variable that is defined within a method or block and can only be used inside that method or block. It is not accessible outside its scope.
Click to reveal answer
beginner
How do you name a local variable in Ruby?
Local variable names must start with a lowercase letter or an underscore (_), followed by letters, digits, or underscores. They cannot start with a capital letter or special characters.
Click to reveal answer
beginner
Why should you use meaningful names for local variables?
Meaningful names make your code easier to read and understand, like giving clear labels to boxes so you know what's inside without opening them.
Click to reveal answer
beginner
Which of these is a valid local variable name in Ruby? <br> A) myVar <br> B) _temp <br> C) 2ndValue <br> D) $globalVar
Both A) myVar and B) _temp are valid local variable names. C) 2ndValue is invalid because it starts with a digit. D) $globalVar is a global variable, not local.
Click to reveal answer
beginner
What happens if you try to use a local variable outside its method or block?
Ruby will raise an error or treat it as nil because the local variable does not exist outside its defined scope.
Click to reveal answer
Which character can a local variable name in Ruby start with?
ALowercase letter or underscore (_)
BUppercase letter
CDigit
DDollar sign ($)
What is the scope of a local variable in Ruby?
AAccessible anywhere in the program
BAccessible only inside the method or block where it is defined
CAccessible only inside classes
DAccessible only in global scope
Which of these is NOT a valid local variable name in Ruby?
AtempVar
B_count
Cuser_name
D3value
Why is it important to use meaningful names for local variables?
ATo make code harder to read
BTo confuse other programmers
CTo make code easier to understand
DTo increase program speed
What symbol indicates a global variable in Ruby, not a local variable?
A$
B#
C_
D@
Explain what a local variable is and how you should name it in Ruby.
Think about where the variable can be used and how its name looks.
You got /3 concepts.
    Why is it important to follow naming conventions for local variables in Ruby?
    Consider how others read your code or how you read your own code later.
    You got /3 concepts.