Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create 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.75

Use 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 puts
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals