Ruby Data Types
The built-in types you'll use constantly -- numbers, strings, booleans, arrays, hashes, and nil.
2 min read
Ruby's core types cover the same ground as most languages, but as objects with a rich set of built-in methods rather than bare primitives.
Numbers
integer = 42
float = 3.14
big = 10_000_000 # underscores as visual separators, ignored by Ruby
puts 7 / 2 # 3 -- integer division truncates
puts 7.0 / 2 # 3.5 -- float division
puts 7.fdiv(2) # 3.5 -- explicit float division without writing 7.0Integer division silently truncating instead of raising an error is one of the most common early gotchas — if you need a precise result, make sure at least one operand is a float, or use fdiv.
Strings
single = 'no interpolation, no escapes except \\ and \''
double = "supports interpolation: #{1 + 1}, and escapes like \t and \n"
"ruby".length # 4
"ruby".upcase # RUBY
" hi ".strip # "hi"
"a,b,c".split(",") # ["a", "b", "c"]Single-quoted and double-quoted strings aren't just a style choice: double quotes process #{} interpolation and escape sequences, single quotes mostly don't. Default to double quotes unless you specifically want the literal text.
Booleans and nil
is_ready = true
is_done = false
result = nilnil represents "no value," similar to null elsewhere. The important quirk: in Ruby, only false and nil are falsy — everything else is truthy, including 0 and "", which are falsy in languages like JavaScript or Python.
puts "truthy!" if 0 # prints -- 0 is truthy in Ruby
puts "truthy!" if "" # prints -- empty string is truthy too
puts "falsy!" unless nil # prints -- nil is one of only two falsy valuesThis trips up almost everyone coming from another language, so it's worth internalizing early.
Arrays and hashes (a preview)
numbers = [1, 2, 3, "four", :five] # arrays can mix types freely
user = { name: "Ada", age: 36 } # a hash: key/value pairsBoth get a full lesson later in the Collections section — for now, just recognize the literal syntax: square brackets for an ordered list, curly braces for key/value pairs.
Checking and converting types
5.is_a?(Integer) # true
5.is_a?(Numeric) # true -- Integer and Float both descend from Numeric
5.class # Integer
"42".to_i # 42
42.to_s # "42"
"3.14".to_f # 3.14Methods ending in ? conventionally return a boolean (is_a?, nil?, empty?) — that's a Ruby-wide naming convention, not special syntax, but it's followed consistently enough that you can rely on it when reading unfamiliar code.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.