Ruby data types – Booleans, Symbols, Numbers

The first question wold be what are “Data Types”?

well a data type is a set of values, and the allowable operations on those values, now in English:

Data types are kind or type of values that you can have and the ways that you can manipulate them. one example using movies would be

Imagine that you are in a “Starswars” movie. here you can use the force, and a lightsaver, you can fight Jedis and siths, this would be your data types and values that you can use, but if you want to use a magic wand and the expectrum patronus to fight a dementor o lord voldemort. well those are not allowed because you are in a Starwars movie, the magic wand and voldermort are part of the “Harry Potter” universe, there might be some things that are similar and some things might have even the same names. but it might work different.

the same way that are different movies with different universes, there are different programming languages like: Ruby, JavaScrip, C++, Phyton, etc, each of this have its own kind of elements and its own way of doing things, and different rules. so in the “Ruby” universe the data types are:

  • Booleans
  • Symbols
  • Numbers
  • Strings
  • Arrays
  • Hashes

And each of this elements have different sub-divisions with its own elements and properties (superpowers), here we will review this data types, its elements and properties.


This type of data is like a coin, it have only two sides: “True” & “False” this can seem very limited at first glance, but just imagine that you have a “Genie in a Bottle” this genie will answer any question that you want with a “True” or “False”, you just need to ask the right questions in the right format and the sky is the limit, one example would be,

Genie does Apple stock will rise tomorro? — Genie answer: True

Genie does the Apple stock will rise more than 20 %?– Genie answer: True

well there you have it, jut put all your savings on Apple stock and tomorrow you will have 20% more money.

in ruby there are some build-in  Methods that will return this type of values, did you remember how we use the answers of the Genie to choose what stock would make us money? the same way we can use this values to guide our decisions and logic in the program.


Symbols  looks like a variable name but it’s prefixed with a colon. examples   :execution   :color.  You dont have to pre-declare a symbol and they are guaranteed to be unique, and will refer to the same object through a ruby program, this is more efficient and save memory because, if you have two strings with the same content, they still two different objects, but a Symbol is the same object all the time.

In Ruby almost everything is an OBJECT and an object has a reference that is stored on a Symbol, most of them you do not create them, Ruby do it by default, and depending how many libraries you have, version of installation and other factors, you can have thousands, and every time that an “Object” is created by you (coding) or by the software runing, a “Symbol” is created by Ruby.  to make this easier to understand in the following example, I will create  one String variable and store on it the name “Andrew Ender Wiggin” from the book and movie “Enders Game”  then I will run and print the following line:

Symbol.all_symbol.size      <— this will return the number of Symbols

as you can see it have 2718 Symbols, now lest add a new string   string_2 = “Maximus Decimus Meridius”    from the movie “Gladiator”  then we ask again how many symbols

Now the number of Symbols went up? but we create a String variable not a Symbol? as we say before: Almost everything in Rubi is an object and whenever an Objects is created Ruby creates a Symbol to store the reference of that Object. and this is what just happen.


basically they are used for “Unique Identifiers” NOT for storing “Data”

one way to make use of it would be with the following rule:

“If the Textual Content of the object is important, use a String. If the Identity of the object is important, use a Symbol

How did we initiate or create a Symbol?

what did we just did? well we create 4 Symbols using using a different way each time.

now the natural questions would be: what this are good for????  what uses this have?  those are fair questions.

we mention before that Symbols are really good for working whit the Identity of an Object, and the following example will help to clarify this concept.


the explanation for the previous code is: we create 3 identicals Strings, then whe ask ruby for the Object Id (the address where is stored) and you can see that Ruby create a new Object for each time that we ask for the Object_Id of the SAME String, and a different address for each, this is a waste of memory, there is a way  to do it better.

This time we create a Symbol and then we ask for the Object Id 3 times and you can see that the Id# stay the same 900708, basically it create it one time and then use it over and over, without waist of memory, in the last case scenario there is not big deal but what if you have millions, there is when you memory management will thank you.

Now the question would be: what is a real life application when I might need to use it?   the following is an example. lest write the code and then we discus it.

Symbols – Example

What we did is we create 2 hashes with the information,of two singers: name & country, with the twist that the keys are Symbols, then we print the Keys and the values, one more thing that we did is print the Object_Id for Keys and Values. as you can see for the values each have a different ID# but the Keys ID# is the same, because we are using a Symbol. this might be very little memory save, but when you have millions of users that is a big difference.

This is just a small example of Symbols in Ruby.


this is a big one, in Ruby we can use numbers, and they are sub divided as:

  • Integer
  • Float
  • Complex
  • BigDecimal
  • Rational

All of those are numbers but they have some differences.and will review a little of each latter on this post.

if you are receiving a number as a parameter and you want to know what kind of number is? well we can ask what class belongs to


and you can also ask other questions about the numbers, you will get a Boolean answer.

“.zero?”  “.nonzero?”  “.integer?”  etc….

I put this on another post but here is again, how cut from the decimals to make the number more easy to read.

Cut to x number or decimals

Random number:

Absolute Value, “.num_abs”


Math with fractions.

Using “nil”




Copyright 2017. All rights reserved.

Posted January 2, 2017 by Edmundo in category "Ruby