Understanding Ruby Data Types

Taci Shlosberg

--

Photo by James Pond on Unsplash

What are data types?

Data types in programming are a classification that specifies the type of data a variable can hold and what type of logical operations can be applied to them

Being new to programming, one of my biggest challenges was understanding which data type I was working with, and how to extract values when the data type was not so simple.

In this post I want to touch on more complex data structures, and how I became more mindful of understanding how to properly read the outcome of what my inputs were. Which in turn led me to understand how to access the correct property to get the desired result.

Below is an explanation of each of the data types I’ve encountered so far and how to work with each one of them.

Common data types include:

Primitive data types:

  • String
  • Integer
  • Boolean
  • Float
  • Symbols

More complex data structures:

  • Hashes/Nested Hashes
  • Arrays/Nested Arrays
  • Classes
  • Objects

Hash: is an associative representation of data, where items are stored by associated keys. The keys are symbols and the values are any data type.

character = {
name: “Chewbacca”,
affiliation: "Rebel Alliance",
weapon: "bowcaster"
}
> character[:name]
=> “Chewbacca”

Hash values can be of any data type, such as a string, a collection of arrays or other hashes. This is called a nested hash.

Nested Hashes:

star_wars = {
"Rebel Alliance": {
"Members": ["Leia Organa", "Han Solo"],
"Droids": ["C-3PO", "R2-D2"]
},
"Jedi Order": {
"Jedi": ["Yoda", "Obi-Wan Kenobi"],
"New Jedi": ["Ben Solo", "Rey"]
}
}

The variable star_wars is a hash with a hash of arrays as children. To access the values of start_wars we can begin by iterating through the first level:

star_wars.each do |group, names|end

If you were to puts group, or puts name, the outcome we get from the code above is:

# if printing group inside of the loop:> group
=> Rebel Alliance
=> Jedi Order
# if printing names inside of the loop> names
=> {:Members=>["Leia Organa", "Han Solo"], :Droids=>["C-3PO", "R2-D2"]}
=> {:Jedi=>["Yoda", "Obi-Wan Kenobi"], :"New Jedi"=>["Ben Solo", "Rey”]}

Let’s iterate through the second level:

star_wars.each do |group, names|
name.each do |key, value|

end
end

This gives us:

# if printing key inside of the name loop> key
=> Members
=> Jedi
# if printing value inside of name loop> value
=> ["Leia Organa", "Han Solo"]
=> ["C-3PO", "R2-D2"]
=> ["Yoda", "Obi-Wan Kenobi"]
=> ["Ben Solo", "Rey”]

Finally we’re going to iterate through the third level:

star_wars.each do |group, names|
name.each do |key, value|
value.each do |name|
end
end
end

The outcome is:

# if printing name inside of the value loop> name
=> Leia Organa
=> Han Solo
=> C-3PO
=> R2-D2
=> Yoda
=> Obi-Wan Kenobi
=> Ben Solo
=> Rey

Arrays: is a collection of items that can be of any type, stored by ordered index.

empire = ["Darth Vader", "Sheev Palpatine", "General Veers"]

If we want to extract “Sheev Palpatine” from the array empire we would do:

> empire[1]
=> ["Sheev Palpatine"]

That’s because the first element gets the index 0, the second gets 1, and so on.

Nested Arrays: as they name says, are arrays with arrays inside of them.

Let’s work with the same example we had in the nested hashes example above.

star_wars = [   ["Rebel Alliance",
["Members",
["Leia Organa", "Han Solo"]
],
["Droids",
["C-3PO", "R2-D2"]
]
],
["Jedi Order",
["Jedi",
["Yoda", "Obi-Wan Kenobi"]
],
["New Jedi",
["Ben Solo", "Rey"]
]
]
]

How would we get the string “R2-D2” ?

> star_wars[0][2][1][1]=> "R2-D2"

Classes: A class is a blueprint from which individual objects are created. To use an analogy, a class is the Death Star blueprint and the object is the actual Death Star that gets created when we make instances or instantiate this blueprint.

class DeathStar
def initialize(commander, has_stupid_major_flaw, weapon)
@commander = commander
@has_stupid_major_flaw = has_stupid_major_flaw
@weapon = weapon
end
end
death_star = DeathStar.new("Wilhuff Tarkin", true, "Superlaser")

Instantiating a new DeathStar triggered the initialize method and the result was the new object being instantiated:

=> #<DeathStar:0x0000563af9373888 @commander="Wilhuff Tarkin", @has_stupid_major_flaw=true, @weapon="Superlaser>

Objects: In Ruby most things are referred to as an object, but in this case, an object is an instance of a class. In our case, the object will be defined as the DeathStar with the commander, has_stupid_major_flaw and weapon as the instance variables.

class DeathStar
def initialize(commander, has_stupid_major_flaw, weapon)
@commander = commander
@has_stupid_major_flaw = has_stupid_major_flaw
@weapon = weapon
end
end
death_star = DeathStar.new
death_star.commander = "Wilhuff Tarkin"
death_star.has_stupid_major_flaw = true
death_star.weapon = "Superlaser"

Symbol: can be used to represent other objects. Symbols are like a string and are created by using a colon before the identifiers and commonly used to represent methods and instance variable names.

class DeathStar
attr_accessor :commander, :has_stupid_major_flaw, :weapon
end

The symbol :commander represents the @commander instance variable in the DeathStar class.

--

--

No responses yet