Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, some JS stuff and more...

Github:
/danielmoralesp
Twitter:
@danielmpbp

2024-09-10

Ruby Data Types: Strings, Numbers and Booleans

Now that we know more about how to install Ruby in Linux or Windows, how to launch the IRB interpreter and what is Ruby programming language, we have the path clear to start programming! 

Let’s launch our IRB console in the Operating System you normally use, and let’s begin





Strings
Strings are the first data type we’ll know inside Ruby. A string is traditionally a sequence of characters. For instance the string: “Hello World!” contains a total of 12 characters (including space and “!” symbol). As we can see is a sequence of characters and in computer programming is considered a data structure because each character is internally separated. Let’s check it out visually:



A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. String may also denote more general arrays or other sequence (or list) data types and structures. Later we’ll see what is an array, for now let’s just test it out in our IRB console

2.6.8 :001 > "Hello World!"


The first rule of the strings in ruby is that they need to be inside quotes (double or single). Once we hit enter, we can see the output of our string

Another way we have to print a string is using the reserved word “puts” or “print”. The puts (short for “put string”) and print commands are both used to display the results of evaluating Ruby code. The primary difference between them is that puts adds a newline after executing, and print does not.

2.6.8 :002 > puts "Hello World!"
2.6.8 :003 > print "Hello World!"


Is Ruby we use frequently the word “puts”

Errors in Ruby
Ruby makes it easy to debug code. Debug is the action of solving errors inside our code. What happens if we copy the word “puts” wrong?

2.6.8 :004 > put "Hello World!"
Traceback (most recent call last):
        4: from /home/daniel/.rvm/rubies/ruby-2.6.8/bin/irb:23:in `<main>'
        3: from /home/daniel/.rvm/rubies/ruby-2.6.8/bin/irb:23:in `load'
        2: from /home/daniel/.rvm/rubies/ruby-2.6.8/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        1: from (irb):4
NoMethodError (undefined method `put' for main:Object)
Did you mean?  puts
               putc


At the beginning it seems pretty annoying and overwhelming, but it’s not. Actually if you see my last screen shot, we have a “NoMethodError” in bold text. Then we got a question: Did you mean? puts. This is very helpful when we’re trying to figure out the error. But what about the Traceback?

The traceback output can be a bit overwhelming if you’re seeing it for the first time or you don’t know what it’s telling you. But the Ruby traceback has a wealth of information that can help you diagnose and fix the reason for the exception being raised in your code. Understanding what information a Ruby traceback provides is vital to becoming a better Ruby programmer

A traceback is a report containing the function calls made in your code at a specific point. Tracebacks are known by many names, including stack trace, stack traceback, backtrace, and maybe others. In Ruby, the term used is traceback.

When your program results in an exception, Ruby will print the current traceback to help you know what went wrong. In our case what went wrong was the word “puts”, it wasn’t well written


It’s fair to say that strings can be used with double or single quotes. The only requisite is that you have to open and close with the same type of quotes.

puts "Hello World!"
puts 'Hello World!'


If you open with double quotes and close with a single quote you’ll have an error (if you execute the file from a text editor). But if you are running Ruby in the IRB interpreter, the line will remain open until you close with the same type of quotes


2.6.8 :013 > puts 'Hello World!
2.6.8 :014'> 
2.6.8 :015'> 
2.6.8 :016'> 
2.6.8 :017'> '


As you can see each line of the terminal was interpreted as spaces.

Later we’ll be working with strings more in detail. For now is enough about strings

Comments in Ruby
- When you’re programming, you will face this kind of situations:
  • - You want to “hide” or “ignore” a line of code from the interpreter, because that line has an error or just because you want to execute just the other code
  • - You want to document your code, because later you may need to understand what you did previously. Or because you’re working with other developers
  • - You want to provide context about your code. This is quite similar to the documentation, but this time the comment is made for yourself. 

The way you can comment a line or lines of code in Ruby is simply using the character “#”

2.6.8 :018 > # puts "Hello World!"
 => nil 
2.6.8 :019 > # printing out a string
 => nil 
2.6.8 :020 > # this line helps us to greet every user in the web page
 => nil 
2.6.8 :021 > puts "Hello World!"

In the command line interpreter (IRB), the output after a comment will always be “nil”. Finally we print the string “Hello World!” again. 


Numbers
Now let’s see another common data type in Ruby: Numbers. There are two types of numeric data in Ruby: Integer and Float
  • - Integer: integer, does not have decimal points. 
  • - Float: can be used for fractional representations.


We all know about the numbers so I think it is not necessary to extend too much here. Let’s just see how it works in the IRB Console

2.6.8 :004 > 1
 => 1 
2.6.8 :005 > 1+2
 => 3 
2.6.8 :006 > 1.2
 => 1.2 
2.6.8 :007 > 1.2 + 3.6
 => 4.8 
2.6.8 :008 > 1.class
 => Integer 
2.6.8 :009 > 1.2.class
 => Float 



At this point I think we know what we’re doing here. Basically we’re summing up to Integers and 2 Floats. The .class method allows us to know what is the parent class of the object. In these cases we got Integer and Float

Other common operators used with number are

2.6.8 :010 > 5*5
 => 25 
2.6.8 :011 > 5**2
 => 25 
2.6.8 :012 > 5**0.5
 => 2.23606797749979 
2.6.8 :013 > 15 % 5
 => 0 
2.6.8 :014 > 15 % 4
 => 3 
2.6.8 :015 > 5 / 4
 => 1 

What do we have here?
  • - On the first line of code we’re multiplying with the operator (*)
  • - Then we’re squaring 5 by 2, here we use (**)
  • - Next we’re calculating the square root with the operator (**0.5)
  • - Then we have the modulo operator, which returns the remainder or signed remainder of a division. We did this with the symbol (%)
  • - And finally we have a simple division with the operator (/)

You can test different values here and then you can check that everything is working fine. 

Booleans

The final data type we’ll be seeing here is the boolean data type. Boolean is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.

The Boolean data type is primarily associated with conditional statements (we’ll be seeing statements in another post), which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. It is a special case of a more general logical data type (see probabilistic logic)—logic doesn't always need to be Boolean

Let’s check it out inside the IRB Console

2.6.8 :016 > true
 => true 
2.6.8 :017 > false
 => false 
2.6.8 :018 > true.class
 => TrueClass 
2.6.8 :019 > false.class
 => FalseClass 


As we can see we can assign directly the value of “true” or “false” in Ruby. Then we can check the parent class with the method “.class”. To see the real effectiveness of this data type we have to see conditionals, which is part of one of the next blog posts

With this we have enough about the basic data types in Ruby

Hope you learned a lot

Thanks for reading!
DanielM