Ruby Data Types

Data types tell your program how to handle what it is that you are telling it to do. Each data type has its own “rules” and is handled in its own specific ways. So let’s dive into each of the data types!

Numbers

There are a couple of sub-types of numbers called integers and floating-point.

Integers are simply numbers without a decimal point and can be either Bignum or Fixnum depending upon the size of the number, however, 99.99999% of numbers you will most likely be handling will be fixnum.

Floating-point numbers are numbers with a decimal point and are commonly referred to as floats. Simple right?

1.0 2.1 3.1415 4.5 5.99

Booleans

Booleans are used to represent either True or False values and are typically found in math operations such as greater than >, less than <, or equal to ==.

greater than:
500 > 100 //true
4 > 235 //false
less than:
794 < 1000 //true
25 < 7 //false
equal to:
5 == 5 //true
794 == 235 //false

Strings

Strings are a sequence of one or more characters (letters, numbers, and symbols) encapsulated by either single '’or double "”quotes.

Double quotes:
"Hello World!"
Single quotes:
'Hello World 24!'

Something else you may come across is string interpolation; this is when you insert a bit of code into a string to be evaluated. When using string interpolation you must use double quotes:

first_name = "Ada"
last_name = "Lovelace"
Double quotes:
puts "Hello #{first_name} #{last_name}!"
// Hello Ada Lovelace!
Single quotes:
puts 'Hello #{first_name} #{last_name}!'
// Hello #{first_name} #{last_name}!

You can also place expressions in string interpolation, variables are not required:

puts "The sum is #{4 + 6}"
// The sum is 10

Symbols

Symbols are like a sibling to strings; they are very similar but are a bit different. They are defined by being preceded by a colon :.

:symbol
:also_a_symbol

They are typically used when the data does not need to be changed because they are immutable (unchangeable) and therefore use less memory which leads to having better performance. Strings, however, are changeable and so would be suitable to use when the data needs to be changed or updated.

Arrays

Arrays are a way to store data or lists of data and can contain all types of data, including other arrays or hashes, however, it is recommended that each array enclose only one type of data. These data sets are enclosed within square brackets [] and each element is separated by a comma ,.

["This", "is", "an", "array"]
[
"This",
"is",
"also",
"an",
"array",
2
]

An array also has an implicit index that begins at 0meaning you don’t need to define your own index number for each element. For the above arrays, the first array would have an index of 0–3 , and the second would have an index of 0–4.

Hashes

Hashes are very similar to dictionaries. A dictionary has a word: definition pair for each entry; a hash as a key: value pair that you define. Hashes can also contain any data type, much like an array. They are defined within curly brackets {} , and each key: value pair is separated by a comma ,.

{key1: value1, key2: value2}{:address => "1234 Maple St", :city => "Seattle", :state => "WA", :zip => 98101}

Typically each key in a pair will be a :symbol so that each pair is unique and easily found within the hash; values in a hash are found via their key, not an index number like in an array.

Hashes are also reminiscent of an unordered list, whereas arrays would be an ordered list, meaning that a hash may not necessarily always be in the same order, but an array will.

Each of the above data types can also be assigned to a variable so that it is easy to recall and re-use in your application.

num = 794
decimal = 3.1415
result = x > 425
words = "I'm a string!"
unchanging = :i_cant_be_changed
myArray = [order1, order2, order3]
myHash = {name: "Kayla", grade: 12, gpa: 3.45}

I hope the above definitions are helpful in your quest to learn to code!

I'm a Full-Stack Software Engineer, with a background in customer service, who recently graduated from Flatiron School. In my down time I love to bake & garden.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store