GGistDev

Values and Types in Ruby

Ruby is dynamically typed; everything is an object. Only false and nil are falsy; everything else is truthy.

Core values

Common literal forms and numeric bases. nil represents “no value.”

true; false; nil
1        # Integer
1.5      # Float
1_000    # separators for readability
0b1010   # binary (10)
0xFF     # hex (255)

Numbers

Integers are arbitrary‑precision; floats are IEEE 754. Ruby also has Rational and Complex.

2**100           # big integer
(22.0/7).round(3)
Rational(1,3) + Rational(1,6)  # => (1/2)
Complex(1, -2) * Complex(0, 1) # => (2+1i)

Booleans and truthiness

Only false and nil are falsy. Zero, empty strings, and empty arrays are truthy.

!!0       # => true
!!""      # => true
!![]      # => true

Strings and symbols

Double‑quoted strings interpolate; symbols are immutable identifiers used as keys or labels.

name = "Ruby"
"Hi #{name}"     # interpolation
:status           # symbol
"10".to_i        # conversion
:role.to_s        # => "role"

Arrays and hashes

Arrays are ordered collections. Hashes map keys to values; keys are commonly symbols, but strings work too.

arr = [1, 2, 3]
person = { name: "Matz", "lang" => "ruby" }
arr << 4
person[:name]   # => "Matz"

Conversions and predicates

Use to_i, to_f, to_s, and predicate methods that end with ?.

"3.1".to_f   # => 3.1
123.to_s     # => "123"
"".empty?    # => true
[1,2].include?(2)  # => true

Everything is an object

Ruby emphasizes duck typing—behavior is about the methods an object responds to.

1.class          # => Integer
"".respond_to?(:empty?)  # => true
nil.nil?         # => true

Summary

  • Core values: booleans, numbers, strings, symbols, arrays, hashes
  • Dynamic typing + duck typing: rely on methods, not explicit type checks