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 = :adminSymbols 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 settingChallenge
EasyCreate 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 = :adminSymbols 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 settingTry it yourself
# TODO: Write your code here
# Create the three symbol variables and display them 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