Data types classify the kind of data you're working with, such as text, numbers, and true/false values. For example,
num = 24
Here, 24 (an integer) is assigned to the num
variable. So the data type of num
is Integer
.
Notice that we haven't explicitly specified the data type of the variable. That's because Ruby automatically infers the data type of the variable. This is known as dynamic typing.
Ruby Data Types
Some common data types in Ruby are listed in the table below:
Data Type | Description | Examples |
---|---|---|
Integer | Whole numbers without decimal points. | 5 , -23 , 1024 |
Float | Numbers with decimal points. | 5.0 , -23.85 , 1024.369 |
String | Textual values enclosed within single or double quotes. | "Hello, World!" , '25' |
Boolean | One of two values: true or false . |
true , false |
Array | Lists of different values. | [1, 2, "hi"] |
Hash | Collections of key-value pairs. | { name: "Ash" } |
nil | Represents "nothing" or "no value." | nil |
Symbol | Lightweight label/identifier. | :hello |
Ruby Numbers (Integer and Float)
An integer is a number without a decimal point.
Meanwhile, a float is a number with a decimal point.
For example,
# Create and print an integer variable
marks = 85
puts marks
# Create and print a float variable
price = 99.99
puts price
Output
85 99.99
Here,
marks
is an integer variable whose value is 85.price
is a float variable whose value is 99.99.
To learn more, visit our tutorial on Ruby Numbers.
More on Numbers
You can use numbers in arithmetic operations. For example,
# Display the sum of 9 and 3
puts 9 + 3
# Display the product of 2.5 and 6.9
puts 2.5 * 6.9
Output
12 17.25
Always remember that performing arithmetic operations on integers will always result in an integer result. For example,
puts 9 / 4
# Output: 2
Here, both 9 and 4 are integers. So 9 / 4
will give us the integer value 2 instead of the actual result, which is 2.25.
To fix this issue, you must use at least one float number in your operation. For example,
# Divide 9.0 (float) by 4 (integer)
puts 9.0 / 4
# Output: 2.25
# Divide 9 (integer) by 4.0 (float)
puts 9 / 4.0
# Output: 2.25
# Divide 9.0 by 4.0
puts 9.0 / 4.0
# Output: 2.25
Ruby String
A string is a piece of text written inside quotes. For example,
message = "Hello, I'm 29 years old."
puts message
age = '29'
puts age
Output
Hello, I'm 29 years old. 29
Here, message
and age
are string variables because they store a sequence of characters enclosed within quotation marks.
Note: You can use both single and double quotes to denote a string. However, we recommend you use double quotes.
To learn more, visit our tutorial on Ruby Strings.
Strings vs. Numbers
Notice that in the previous program, the age
variable is storing the string '29'
. Even though we understand this to be a number, Ruby interprets it as a string because the value has quotations around it.
# String value because of single quotes
age_string = '29'
puts age_string
# String value because of single quotes
price_string = "99.99"
puts price_string
# Integer value (no quotations present)
age_int = 29
puts age_int
# Float value (no quotations present)
price_float = 99.99
puts price_float
Output
29 99.99 29 99.99
Here,
'29'
and"99.99"
are strings because they have single and double quotes enclosing the number values.- 29 and 99.99 are numbers because they are not enclosed by quotation marks.
Note: The puts
method prints strings without displaying the quotation marks. Thus, the output of both puts '29'
and puts 29
will be 29
.
Ruby Boolean
Boolean values can be either true
or false
. For example,
# Print boolean values
puts true
puts false
# Create boolean variables
can_vote = true
can_drink = false
# Print the boolean variables
puts can_vote
puts can_drink
Output
true false true false
Note: Booleans are used in conditional statements and loops (which we will learn in later chapters).
To learn more, visit Ruby Boolean.
Ruby Array
Arrays are a list of values. For example,
array_int = [1, 2, 3]
puts array_int
Output
1 2 3
Here, array_int
is an array that consists of three integer values: 1, 2, and 3.
You can create arrays of other types as well. You can also mix data of different types into a single array. For example,
# An array of floats
array_float = [1.1, 2.2, 3.3]
p array_float
# An array of strings
array_string = ["Black Sabbath", "Deep Purple", "Led Zeppelin"]
p array_string
# Mixed array
array_mixed = ["Ash Ketchum", 10, false]
p array_mixed
Output
[1.1, 2.2, 3.3] ["Black Sabbath", "Deep Purple", "Led Zeppelin"] ["Ash Ketchum", 10, false]
Tip: Use p
instead of puts
when printing arrays or other complex data structures. It shows the full structure (including quotes, brackets, etc.).
To learn more, visit our tutorial on Ruby Arrays.
More on Arrays
You can access individual array elements using indexes, i.e., numerical values that indicate the position of the element.
Array indexes start from 0, which means:
- 0 gives the position of the first element.
- 1 gives the position of the second element.
- And so on.
For example,
rock_bands = ["Black Sabbath", "Deep Purple", "Led Zeppelin"]
# Display the first element
puts rock_bands[0]
# Display the second element
puts rock_bands[1]
# Display the third element
puts rock_bands[2]
Output
Black Sabbath Deep Purple Led Zeppelin
Here, rock_bands
is an array, and we accessed each element of this array using the following syntax:
rock_bands[index]
Thus,
rock_bands[0]
gives us the first element, i.e.,"Black Sabbath"
.rock_bands[1]
gives us the second element, i.e.,"Deep Purple"
.rock_bands[2]
gives us the third element, i.e.,"Led Zeppelin"
.
Ruby Hash
A hash stores pairs of keys and values. It is similar to the dictionary type in other programming languages. For example,
# Create a hash named 'student'
student = {
"id" => 121,
"name" => "Ash Ketchum",
"age" => 10
}
# Print the value stored in key "id"
puts student["id"]
# Print the value stored in key "name"
puts student["name"]
# Print the value stored in key "age"
puts student["age"]
Output
121 Ash Ketchum 10
Here, student
is a hash that stores the following key-value pairs:
Key | Value |
---|---|
"id" |
121 |
"name" |
"Ash Ketchum" |
"age" |
10 |
Notice that:
- Values are assigned to keys using the
=>
symbol. - We access values by specifying the key. For instance,
student["id"]
gives the value of the"id"
key (121) that's stored thestudent
hash.
To learn more, visit Ruby Hashes.
Ruby nil
The nil
value in Ruby means "nothing." It's Ruby's way of saying "no value here."
name = nil
puts name
This program doesn't print anything, because nil
means the variable has no value.
Ruby Symbol
A symbol is a lightweight, immutable label in Ruby. It's often used in situations where you don't need the features of a full string, such as keys in hashes.
Symbols start with a colon (:
). For example,
my_symbol = :hello
puts my_symbol
# Output: hello
Here, my_symbol
is a variable that stores the symbol :hello
.
Notice that the puts
method doesn't display the colon when printing the symbol.
Use Symbols as Hash Keys
You can also use symbols as hash keys. For example,
student = {
id: 121,
name: "Ash Ketchum",
age: 10
}
puts student[:id]
puts student[:name]
puts student[:age]
Output
121 Ash Ketchum 10
Here, the symbols :id
, :name
, and :age
are keys inside the student
hash.
Notice that we've used the colon at the end of the label when defining the hash.
student = {
id: 121,
name: "Ash Ketchum",
age: 10
}
But then, we used the colon at the beginning when printing the values of the keys.
puts student[:id]
puts student[:name]
puts student[:age]
This is because Ruby 1.9 introduced a shorthand syntax to make it easy to create key-value pairs using symbols.
The older, less convenient syntax requires you to use symbol keys in the following way:
student = {
:id => 121,
:name => "Ash Ketchum",
:age => 10
}
As you can see, the new syntax is much more convenient, even though it can be confusing for beginners.
Frequently Asked Questions
You can check a variable or literal's type using .class
. For example,
puts "hello".class
# Output: String
puts :hello.class
# Output: Symbol
puts 123.class
# Output: Integer
puts 12.3.class
# Output: Float
puts true.class
# Output: TrueClass
puts false.class
# Output: FalseClass
puts nil.class
# Output: NilClass
my_array = [1, 2, 3]
puts my_array.class
# Output: Array
my_hash = { name: "Ragnar Lothbrok" }
puts my_hash.class
# Output: Hash
Note that Ruby doesn't have a Boolean
class. Instead, true
and false
are instances of TrueClass
and FalseClass
, respectively.
Yes, Ruby supports a few other data types as well, such as:
- Ranges: Represent a sequence of values (e.g.,
1..5
). - Complex Numbers: Represent numbers with real and imaginary parts.
- Rational Numbers: Represent exact fractions (e.g.,
1/2
instead of 0.5).
Let's look at a simple example of complex and rational numbers:
require 'complex'
puts Complex(2, 3) # Output: 2+3i
puts Rational(1, 2) # Output: 1/2