TExcelTAD
Home › Ruby › Ruby Data Types

Ruby Data Types

Every value in Ruby is an object, and every object belongs to a class. The class decides which methods the value has. You never declare types — Ruby figures them out from the values themselves.

The built-in types

Type (class) Example Used for
Integer 42, -7, 1_000_000 Whole numbers of any size
Float 3.14, -0.5, 2e3 Decimal numbers
String "hello", 'hi' Text
Symbol :name, :active Lightweight names/labels
TrueClass / FalseClass true, false Booleans
NilClass nil "No value"
Array [1, "two", :three] Ordered lists
Hash {name: "Ada", age: 36} Key-value pairs
Range 1..10, "a".."e" Sequences of values
Rational, Complex 3/4r, 2+3i Exact fractions, complex numbers

Finding the type of a value

Call .class on any value:

ruby · try it yourself
p 42.class
p 3.0.class
p "text".class
p :label.class
p true.class
p nil.class
p [1, 2].class
p({a: 1}.class)
p (1..5).class
Integer
Float
String
Symbol
TrueClass
NilClass
Array
Hash
Range

Checking a type

Use is_a? (also called kind_of?) to ask whether a value is of a certain class or one of its subclasses. instance_of? checks for the exact class only:

ruby · try it yourself
n = 10
puts n.is_a?(Integer)
puts n.is_a?(Numeric)     # Integer is a kind of Numeric
puts n.instance_of?(Numeric)
puts "hi".is_a?(Comparable)
puts n.respond_to?(:even?)
true
true
false
true
true

respond_to? asks "does this object have a method with that name?" — often a better question than "what class is it?". This idea, called duck typing, has its own lesson later.

Everything is an object — even classes

Classes themselves are objects (of class Class), and you can explore the family tree with ancestors:

ruby · try it yourself
p Integer.class
p Integer.ancestors.take(4)
p 5.methods.include?(:times)
Class
[Integer, Numeric, Comparable, Object]
true

Converting between types

Conversion methods start with to_:

ruby · try it yourself
p "42".to_i
p "3.7".to_f
p "12abc".to_i
p "abc".to_i
p 99.to_s
p 2.9.to_i
p :done.to_s
p "ready".to_sym
p [[:a, 1], [:b, 2]].to_h
p (1..4).to_a
42
3.7
12
0
"99"
2
"done"
:ready
{a: 1, b: 2}
[1, 2, 3, 4]

The to_ methods are forgiving: "abc".to_i is simply 0. When bad input should be an error, use the strict conversion functions Integer() and Float():

ruby · try it yourself
p Integer("250")
p Float("1.5")

begin
  Integer("25 apples")
rescue ArgumentError => e
  puts "Error: #{e.message}"
end
250
1.5
Error: invalid value for Integer(): "25 apples"
Method "12abc" "abc" nil
.to_i 12 0 0 (nil.to_i)
Integer(...) error error error

Mutable and immutable values

Numbers, symbols, true, false and nil are immutable — there is exactly one 5 and one :ok in the whole program. Strings, arrays and hashes are mutable: methods like << and push change them in place.

ruby · try it yourself
p :ok.object_id == :ok.object_id
p "ok".object_id == "ok".object_id
true
false

Two equal symbols are the very same object; two equal string literals are separate objects.

Tip: nil is a real object too: nil.to_s is "", nil.to_a is [] and nil.inspect is "nil".

Test yourself

What is the class of 3.0?

  1. Integer
  2. Float
  3. Decimal
  4. Numeric

What does "7 dwarfs".to_i return?

  1. 7
  2. 0
  3. nil
  4. It raises an error

Which statement is true for 5.is_a?(Numeric)?

  1. It returns false because 5 is an Integer
  2. It returns true because Integer inherits from Numeric
  3. It raises an error
  4. It returns nil