Numbers & Strings
Ruby is dynamically typed. Everything is an object — even integers, nil, and true. Strings are mutable; numbers come in Integer, Float, Rational, Complex.
Common types in action
EXAMPLE
# Numbers
puts 1.class # Integer
puts 1.0.class # Float
puts (1/3r).class # Rational → 1/3
puts 1_000_000.to_s # underscores ignored
# Strings — mutable, interpolation with double quotes
name = 'Ada'
greeting = "Hello, #{name}!"
puts greeting.upcase
# Symbols — immutable, interned, perfect for keys
status = :active
puts status.object_id == :active.object_id # true
# Arrays + Hashes — JSON-shaped
xs = [1, 2, 3]
h = { name: 'Ada', age: 36 } # symbol keys via shorthand
# Range
(1..5).to_a # [1, 2, 3, 4, 5]
('a'..'e').to_a # ['a','b','c','d','e']
# nil and true / false
puts nil.class # NilClass
puts nil.respond_to?(:nil?) # true — even nil is an object
Why it matters
Ruby’s “everything is an object” means you can chain methods anywhere. nil&.foo&.bar safely no-ops; (arr || []).each { … } survives a missing array.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…