Data Types and Variables in Ruby
Fast recall
Quick revision
Core answer
nil. A variable's prefix communicates scope: local name, instance @name, class @@name, global $name, or constant NAME.Whether you know Ruby is dynamically typed, the core types (Integer/String/Symbol/Array/Hash/nil), and the variable scopes (local/instance/class/global/constant).
Checking types with == (use .class or duck typing), or confusing symbol (:a) and string ('a') keys.
Ruby is dynamically typed. Types: Integer, Float, String, Symbol, Array, Hash, Range, Boolean, nil. Vars: local x, @x instance, @@x class, $x global, UPPER const. Everything is an object.
Interview-ready guide
Interview answer
Ruby is dynamically typed. A variable is a name that refers to an object; the object has a class and provides the available methods. Ruby does not declare a variable as int count. After count = 3, the name refers to an Integer, and a later assignment can make it refer to a String.
Common Ruby objects include Integer, Float, String, Symbol, Array, Hash, Range, true, false, and nil. Every value is an object, so even 3.times is a method call. Ruby often uses duck typing: code asks for the required behaviour instead of repeatedly checking the object's exact class.
Assignment does not copy a mutable object. After backup = items, both names refer to the same Array, so items << "book" is visible through backup. Later, items = [] only points items at a different Array; it does not change what backup refers to.
Ruby spelling communicates scope: total is local, @total belongs to one object, @@total is shared through a class hierarchy, $total is global, and TOTAL is a constant. Constants express an intention not to reassign; Ruby can still reassign one with a warning. Only nil and false are falsey—zero and empty collections are truthy.
Data Types and Variables in Ruby
Direct Answer
Ruby is dynamically typed: variables do not have declared types; they hold references to objects, and the object determines the available behaviour. Common objects include integers, floats, strings, symbols, arrays, hashes, ranges, booleans, and nil. A variable's prefix communicates scope: local name, instance @name, class @@name, global $name, or constant NAME.
Deep Dive & Architecture
An assignment changes a binding between a name and an object. It does not pour a new type into a variable. This explains why value = 10 followed by value = 'ten' is valid, and why aliases to the same Array observe the same mutation. Method arguments follow the same model: Ruby passes the object reference as a value, so a method can mutate the referenced object but cannot rebind the caller's local variable.
Object behaviour matters more than labels. Integers respond to arithmetic, Strings to text operations, and collections to iteration. Duck typing takes that one step further: a method can accept any object that responds to the required messages. render(item) may need only item.to_s, not a specific inheritance tree.
Variable spelling creates scope. A local name lives in its lexical context. An instance variable is stored on a particular object and begins as nil if it has not been assigned. Class variables are shared with descendants and can surprise callers, so class-instance variables are often safer for independent class state. Globals are visible broadly and make dependencies difficult to follow.
Ruby truthiness is separate from type. Only nil and false are falsey; zero, empty Strings, and empty Arrays are truthy. Type checks such as is_a? are still appropriate at system boundaries, but ordinary collaboration is clearer when code asks for the operation it needs.
Code Example
value = 3
value = 'three' # same local name, different object
items = []
backup = items # both names reference one Array
items << 'book' # mutate that Array
backup # => ['book']
items = [] # rebind only items
backup # => ['book']
class Cart
LIMIT = 20 # constant naming expresses intent
def initialize
@items = [] # state on this Cart object
end
end
Common Mistakes & Pitfalls
Checking types with == (use .class or duck typing), or confusing symbol (:a) and string ('a') keys.
Check your understanding
Answer these without looking back at the article.
- When would you use Data Types Variables in a real project?
- How would you compare Data Types Variables with an alternative?
- What is a common mistake when using Data Types Variables?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.