Limited Offer
60% OFF on all plans!

Variables and Constants in Ruby

Understand how to declare and use local variables, global variables, instance variables, class variables, and constants in Ruby.

Introduction to Variables

A variable is a named container for a value. Ruby uses naming conventions to determine a variable's scope: local, global, instance, class, or constant.

1. What Is a Variable?

Variables store data that your program can read and change as it runs.

2. Naming Conventions

Ruby uses the variable's first character(s) to signal its scope.

Declaring Variables

Ruby variables are created the moment you assign a value. There is no `var`, `let`, or `const` keyword — just write the name and use `=`.

1. Simple Assignment

Use `=` to assign a value. Ruby infers the type automatically.

2. Multiple Assignment

Assign multiple variables in one line using parallel assignment.

3. Conditional Assignment (||=)

`||=` assigns a value only if the variable is nil or false.

Constants

Constants in Ruby start with an uppercase letter. Ruby will warn you if you reassign a constant, but unlike other languages, it does NOT raise a hard error by default.

1. Defining Constants

Use ALL_CAPS for true constants and PascalCase for class/module constants.

2. Reassigning a Constant

Ruby issues a warning when you reassign a constant, but does not stop execution.

Mutability vs Reassignment

In Ruby, variables hold references to objects. Reassigning a variable changes what it points to. Mutating changes the object itself.

1. Reassignment vs Mutation

Reassignment makes the variable point to a new object. Mutation changes the existing object in place.

2. Freezing Objects

`freeze` makes an object immutable. Any attempt to mutate it raises a `FrozenError`.

Variable Scope

Ruby scope is determined by where a variable is defined. Local variables are confined to their block/method. Global variables are accessible everywhere.

1. Local Scope

Local variables are only accessible within the method or block where they are defined.

2. Global Variables ($)

Global variables start with `$` and are accessible from anywhere in the program.

Best Practices

Following Ruby conventions makes your code more readable and maintainable.

1. Naming Rules

Use descriptive names. Follow Ruby's `snake_case` for variables and methods.

2. Avoid Global Variables

Global variables pollute the namespace and make debugging hard. Prefer constants or passing values as arguments.

Symbols in Ruby

A Symbol in Ruby is an immutable, reusable identifier written with a leading colon (`:name`). Unlike strings, symbols with the same name are always the exact same object in memory — making them ideal for keys, options, and identifiers.

1. What Is a Symbol?

A symbol is created with a colon prefix. It is immutable and always refers to the same object in memory.

2. Symbol vs String

Symbols and strings look similar but behave very differently.

3. Symbols as Hash Keys

The most common use of symbols in Ruby is as hash keys. Ruby even provides a shorthand syntax for it.

4. Converting Between Symbol and String

Ruby makes it easy to convert back and forth between symbols and strings.

5. Symbols as Method Identifiers

Ruby uses symbols to refer to method names — for example in `respond_to?`, `send`, and `method`.

6. %i[] — Symbol Array Shorthand

`%i[]` creates an array of symbols without writing each colon manually.