Menu
Coddy logo textTech

Symbol Data Type

Part of the Fundamentals section of Coddy's Ruby journey — lesson 7 of 88.

In Ruby, symbols are a unique data type that represent immutable identifiers. A symbol is created by prefixing a name with a colon :. Unlike strings, symbols with the same name always refer to the same object in memory, making them more memory-efficient.

Creating a symbol variable follows this pattern:

status = :active
color = :red
role = :admin

Symbols look similar to strings but serve a different purpose. While strings are meant for text that might change or be manipulated, symbols are used for identifiers, labels, and keys that remain constant. They are commonly used as hash keys and for representing states or categories.

Examples of symbols in Ruby:

status = :pending     # Represents a state
day = :monday         # Represents a day
mode = :dark_mode     # Represents a setting
challenge icon

Challenge

Easy

Create three symbol variables to represent different task states. First, create a variable called current_state and assign it the symbol :active. Then create a variable called next_state and assign it the symbol :pending. Finally, create a variable called final_state and assign it the symbol :completed. Display all three variables using the puts command.

Cheat sheet

In Ruby, symbols are immutable identifiers created by prefixing a name with a colon :. They are memory-efficient as symbols with the same name refer to the same object in memory.

Creating symbol variables:

status = :active
color = :red
role = :admin

Symbols are used for identifiers, labels, and keys that remain constant, such as hash keys, states, or categories:

status = :pending     # Represents a state
day = :monday         # Represents a day
mode = :dark_mode     # Represents a setting

Try it yourself

# TODO: Write your code here
# Create the three symbol variables and display them 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