HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›Variables

Variables

Creating variables, naming, and constants (UPPERCASE).

Simple enough for a 5-year-old to understand

Variable definition

Example code
1
name = "Alex"
Text.
VariableText.
2
age = 36
A number.
VariableA number.
3
puts "#{name} - #{age}"
Alex - 36
Output / InputAlex - 36
VariableOutput / Input
💡 Hint: Variables start with a lowercase letter: username (snake_case is common).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Assign "Antalya" to the city variable and print it.

🎯 Expected output: Antalya

Loading editor…

About this topic

In Ruby, you don't need to write keywords or types to define a variable: you assign a value to a name, and the variable is created. The type is held in the VALUE, not the variable, so different types of values can be assigned to the same variable.

Ruby considers the first character of a variable name meaningful. A name starting with a lowercase letter is a local variable; one starting with an uppercase letter is a CONSTANT; one starting with an at sign is an instance variable; one starting with a dollar sign is global. The name also indicates the scope.

A behavior of Ruby to watch out for is that assignment does NOT produce a copy. Two variables can point to the same object, and a change made through one is visible in the other. This leads to unexpected results with strings and arrays.

What you will learn

  • Defining variables and not needing to declare types
  • That naming rules determine the scope
  • That reassigning constants gives a warning
  • That assignment does not produce a copy

When to use?

Values that should not change are written in uppercase. Ruby allows reassignment but prints a warning; this is the way to state intent in the code.

Global variables are a tool to be avoided in almost all cases: it becomes impossible to track where they were modified.

Common mistakes

Wrong

a = "hello"
b = a
b << " world"
puts a

Assignment does not produce a copy; a and b point to the SAME string object. Since the in-place appending operator modifies the object, a also changes and "hello world" is printed to the screen. If you want an independent copy, you need to use dup.

Correct

a = "hello"
b = a.dup
b << " world"
puts a

Wrong

puts total
total = 5

A variable must be assigned before it is used; the interpreter gives an "undefined local variable or method total" error. Since Ruby executes code from top to bottom, the assignment line has not been processed yet. This is different from the concept of prototyping in compiled languages.

Correct

total = 5
puts total

Wrong

LIMIT = 100
LIMIT = 200
puts LIMIT

Names starting with an uppercase letter are constants, and reassignment produces an "already initialized constant LIMIT" warning. Ruby does not stop the program, it performs the assignment and continues; meaning constants are advisory. It is necessary to heed the warning.

Correct

LIMIT = 100
current = 200
puts "#{LIMIT} #{current}"

Frequently asked questions

Can I assign different types of values to a variable?

Yes, Ruby does not prevent this because the type is held in the value. However, using the same variable first as a number and then as text makes the code hard to read and leads to unexpected errors. It is a good habit for each variable to have a single meaning.

What is nil?

It is the object representing the absence of a value. There are only two values considered false in Ruby: nil and false. Zero, empty text, and empty array are TRUE — this is different from many languages and is the Ruby rule that causes the most confusion.

When are variables starting with an at sign used?

Inside a class, to hold the state of the object. An instance variable is accessible from all methods of that object and exists as long as the object lives. A local variable, on the other hand, disappears when the method it is defined in finishes.

Summary

No keywords or types are needed to define a variable.

The first character of the name determines the scope: lowercase is local, uppercase is constant.

Assignment does not produce a copy; two variables can point to the same object.

Every value except nil and false is considered true.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code