Naming Conventions
Part of the Fundamentals section of Coddy's Ruby journey — lesson 9 of 88.
Ruby has specific rules for valid variable names. A variable name must start with a lowercase letter (a-z) or an underscore (_), and can contain lowercase letters, numbers, and underscores. However, it cannot start with a number, contain spaces, or use special characters.
Names starting with uppercase letters are treated as constants in Ruby.
Beyond the basic rules, following naming conventions makes your code much more readable and maintainable.
Ruby conventionally uses snake_case for variable names (using underscores between words like user_name). This is the standard Ruby style and is widely adopted by the Ruby community. Consistency with this convention makes your code look professional and familiar to other Ruby developers.
Most importantly, use descriptive names that clearly indicate what the variable contains. Instead of x or data1, use names like student_score or monthly_sales. This makes your code self-documenting and easier for others (and your future self) to understand.
Challenge
EasyCreate three variables following Ruby's snake_case naming convention. Create a variable for an employee's full name and assign it "John Doe", a variable for their age and assign it 25, and a variable for their monthly salary and assign it 1500.50. Display all three variables using puts.
Cheat sheet
Ruby variable names must start with a lowercase letter (a-z) or an underscore (_), and can contain lowercase letters, numbers, and underscores. They cannot start with a number, contain spaces, or use special characters.
Ruby uses snake_case naming convention for variables (words separated by underscores):
user_name = "Alice"
student_score = 95
monthly_sales = 2500.75Use descriptive names that clearly indicate what the variable contains, rather than generic names like x or data1.
Try it yourself
# TODO: Create three variables following Ruby naming conventions
# 1. A variable for an employee's full name (string)
# 2. A variable for their age (integer)
# 3. A variable for their monthly salary (float)
# TODO: Display all three variables using putsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False