Variables and Types in Ruby
Learn about variables, data types, symbols, and type conversions in Ruby with practical Docker-ready examples
Ruby is a dynamically and strongly typed language — you never declare a variable’s type, but Ruby always knows what type a value is and won’t silently coerce incompatible ones. This gives you the flexibility of a scripting language with the safety of a strong type system.
As a multi-paradigm language, Ruby treats everything as an object. That means integers, strings, booleans, and even nil all have methods you can call on them. There’s no distinction between primitive types and objects — everything participates in the same object model.
In this tutorial you’ll learn how Ruby’s type system works in practice: how to assign variables, what types are available, how to check types at runtime, and how to convert between them.
Variable Assignment
Ruby uses simple assignment with no type annotations. Variable names follow lowercase conventions with underscores for multiple words.
Create a file named variables.rb:
| |
String interpolation with #{} is idiomatic Ruby — it calls .to_s on whatever is inside the braces and embeds the result directly in the string.
Running with Docker
| |
Expected Output
Name: Alice
Age: 30
Pi: 3.14159
Happy: true
Nothing: nil
Max: 100, Language: Ruby
Status: active, Role: admin
Integer
String
Float
TrueClass
NilClass
Symbol
Core Types
Ruby’s built-in types cover everything you need for everyday programming:
| Type | Example | Notes |
|---|---|---|
Integer | 42, -7, 1_000_000 | Arbitrary precision; underscores allowed for readability |
Float | 3.14, -0.5 | 64-bit IEEE 754 double |
String | "hello", 'world' | Mutable; double-quoted strings support interpolation |
TrueClass / FalseClass | true, false | Each is a singleton object |
NilClass | nil | The absence of a value; also a singleton |
Symbol | :active, :name | Immutable identifier; same symbol is always the same object |
Array | [1, "two", :three] | Ordered, mixed-type, dynamic length |
Hash | { name: "Alice", age: 30 } | Key-value pairs; symbol keys are most common |
Type Conversions
Ruby uses explicit conversion methods rather than automatic coercions. Each type provides a family of to_* methods.
Create a file named type_conversion.rb:
| |
Running with Docker
| |
Expected Output
42.0
42
42/1
3
4
4
100
3.14
42
0
true
false
true
false
Error: invalid value for Integer(): "abc"
Variable Scope Prefixes
Ruby uses naming prefixes to signal variable scope — no keywords like var or let are needed:
| Prefix | Example | Scope |
|---|---|---|
| (none) | count = 0 | Local to current method or block |
@ | @name = "Alice" | Instance variable (per object) |
@@ | @@total = 0 | Class variable (shared across all instances) |
$ | $debug = false | Global variable (avoid in most code) |
| UPPERCASE | PI = 3.14159 | Constant (warns on reassignment) |
For now, focus on local variables and constants — instance and class variables become relevant when you start writing classes.
Key Concepts
- No type declarations — Ruby infers types from values; assignment is just
x = value - Everything is an object — even
42andnilhave methods (.class,.to_s,.inspect) - Symbols are not strings —
:nameand"name"are different types; symbols are immutable and memory-efficient for identifiers nilis an object — not a null pointer; it’s an instance ofNilClasswith its own methodstrueandfalseare singletons —TrueClassandFalseClasseach have exactly one instance- Explicit conversions — use
.to_i,.to_f,.to_s; preferInteger()/Float()when you want an error on bad input is_a?checks type,respond_to?checks capability — the latter is the idiomatic Ruby (duck typing) approach- String interpolation —
"Hello, #{name}!"is the idiomatic way to build strings; single-quoted strings do not interpolate
Comments
Loading comments...
Leave a Comment