LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 419 min read

Variables & Data Types

Learn Ruby's core data types, string interpolation, Ruby's distinctive Symbol type, and the different variable scope prefixes.

Introduction

Ruby is dynamically typed, meaning a variable's type is determined by whatever value it currently holds, not declared up front. This lesson covers Ruby's core types, its clean string interpolation syntax, the Symbol type that surprises newcomers from other languages, and how variable name prefixes control scope.

What You Will Learn
  • Ruby's core data types and how dynamic typing works in practice.
  • How string interpolation embeds expressions directly inside a string.
  • What a Symbol is and why Ruby code uses them constantly.
  • How variable name prefixes ($, @, @@) determine scope.

Core Data Types

TypeExampleNotes
Integer42Whole numbers, with no separate "long" or "short" distinction.
Float3.14Decimal numbers.
String"hello"Mutable text, unlike Lua's immutable strings.
Symbol:statusA lightweight, immutable, named identifier — covered fully below.
true / falsetrue, falseRuby's two boolean values, each their own actual object.
nilnilRepresents "no value" — Ruby's equivalent of null or None.
Array[1, 2, 3]An ordered, resizable list — covered fully in lesson 10.
Hash{name: "Amit"}A key-value map — covered fully in lesson 10.
age = 29
price = 19.99
name = "Neha"
active = true
puts age.class # Integer
puts price.class # Float
puts name.class # String
puts active.class # TrueClass
Output

Click Run to see what this code prints.

Even true Has a Class

That last line — active.class returning TrueClass — is a direct preview of lesson 5's big idea: in Ruby, even true and false are real objects belonging to their own classes, not special-cased primitives the way they are in many other languages.

String Interpolation

Inside a double-quoted string, #{expression} embeds the result of any Ruby expression directly — the idiomatic alternative to string concatenation with +.

name = "Kabir"
score = 87
puts "#{name} scored #{score} points, which is #{score >= 60 ? "a pass" : "a fail"}."
Output

Click Run to see what this code prints.

Interpolation only works inside double quotes ("..."); single-quoted strings ('...') treat #{...} as literal text, with no interpolation performed.

Symbols: A Distinctive Ruby Type

A Symbol, written with a leading colon (like :name or :active), is a lightweight, immutable identifier — think of it as a name that refers to itself, used constantly for things like Hash keys, method names, and configuration options, in places where a String would work but waste memory and comparison speed on something that's really just a fixed label.

status = :active
puts status.class # Symbol
puts status == :active # true
puts :active.object_id == :active.object_id # true -- always the same object
Output

Click Run to see what this code prints.

Why Not Just Use Strings?

Two identical symbols (:active and :active) are always literally the same object in memory, while two identical strings ("active" and "active") are typically two separate objects — making symbols faster to compare and a natural fit for Hash keys and internal labels that never need to be mutated like real text.

nil, true, and false

Ruby's truthy/falsy rule is simple and has no surprises: only nil and false are falsy. Every other value — including 0 and "" (an empty string) — is truthy, the same rule Lua uses if you've taken that course on this platform.

if 0
puts "0 is truthy in Ruby!"
end
if ""
puts "empty string is truthy too!"
end
Output

Click Run to see what this code prints.

Variable Scope Prefixes

Ruby uses a variable's name prefix (rather than a separate declaration keyword) to indicate its scope.

PrefixScopeExample
(none)Local variable — visible only in the current method/block.count = 0
@Instance variable — belongs to one specific object.@name = "Amit"
@@Class variable — shared across all instances of a class.@@total_count = 0
$Global variable — visible everywhere in the program (used sparingly).$debug_mode = true

Common Mistakes

Avoid These Mistakes
  • Assuming 0 or "" is falsy, coming from a language like Python or JavaScript — in Ruby, only nil and false are falsy.
  • Using single quotes when you actually need string interpolation — #{...} does nothing inside single-quoted strings.
  • Overusing global variables ($name) when a local, instance, or class variable would scope the value more appropriately.

Best Practices

  • Default to Symbols (:like_this) for fixed internal labels, Hash keys, and configuration options rather than Strings.
  • Use double-quoted strings when you need interpolation, and single-quoted strings for plain literal text — a common style convention.
  • Reserve global variables ($) for genuinely program-wide state, which should be rare in well-organized Ruby code.

Frequently Asked Questions

No — Ruby is dynamically typed, so a variable's type is determined by whatever value it currently holds, and can change over its lifetime.

No — they look similar in output but are a distinct, immutable type optimized for identity comparison, not text manipulation. Convert between them with .to_sym and .to_s when needed.

An instance variable (@name) belongs to one specific object and can differ between instances; a class variable (@@count) is shared across every instance of a class, covered further in lesson 9.

Key Takeaways

  • Ruby is dynamically typed; a variable's type comes from its current value.
  • Double-quoted strings support #{expression} interpolation; single-quoted strings do not.
  • A Symbol (:name) is a lightweight, immutable identifier, distinct from a String.
  • Only nil and false are falsy in Ruby — everything else, including 0 and "", is truthy.
  • A variable's prefix ($, @, @@, or none) determines its scope.

Summary

You now know Ruby's core types, its interpolation syntax, and the Symbol type that shows up constantly in idiomatic Ruby code. Next, you'll dig into the single idea that shapes everything else in this course: in Ruby, everything really is an object.

Next Lesson →

Everything Is an Object