First steps with Ruby
Tags: ruby, codenewbie, beginners
Published: 2020-10-22 07:44:16

Dear readers and friends. Here I present the next article about Ruby PL. The previous article was just an introduction to Ruby PL [(Introducing Ruby programming language)] (https://dev.to/eminarium/introducing-ruby-programming-language-27o4). Here are the keynotes from the previous article about Ruby :
* Ruby was first developed by Yukihiro Matsumoto and was first introduced in 1995.
* Ruby is an interpreted language (it means, it is not converted to binary/machine code, but it is analyzed and run by a special program called “interpreter” and the result is presented to the user).
* Ruby is cross-platform (it means, it can be run on various platforms like Mac, Windows, Linux, and Unix based OS).
* Official page of Ruby PL: https://www.ruby-lang.org/
* Official Github page of Ruby PL: https://github.com/ruby/ruby

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9de6eq9linced4tg6www.jpeg)

Simplicity, ease of learning and its performance are the key factors to its fast increase in popularity. As some of the learners cite about Ruby “I can easily understand what is going on in the program even before learning the language itself”. It can be easily learned in a short period and became popular in software developer communities very fast because of its syntax resembling regular English language. Being able to use Ruby as a Frontend (client-side) tool and as a Backend tool (server-side) just adds extra coins to its popularity.  

As it is easy to learn and use for daily tasks, made programmers take it even further. Developers and contributors developed and added up “Sinatra” and “Ruby on Rails” to a list of widely known and widely used frameworks that let us develop full-scale web applications. Among these, we have to bring the “Ruby on Rails” framework to the spotlight, as a tool being enormously popular and being used by the largest platforms around the world.  
  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5rbaw3ozkfmu771qbdwl.jpg)  
  

As you all know, any data is related to one of the data types in the programming world. For instance, price: a number, surname: a string, food I like: array and etc. A working program is created by correctly gathering these pieces and processing them. And now, let’s look at the data types used in Ruby programming language. Primary data types in Ruby are the following:

* Numbers (any number, integer, or float)
* Strings (set of characters)
* Booleans (data type having only 2 states, open/closed, passed/failed)
* Arrays (ordered list of items)
* Hashes (like Arrays, but they have keys instead of index numbers)
* Symbols (data type representing other objects)  
Now let’s look at each of the above-mentioned data types separately.    
  
# Data Types      

**Numbers**: This data type is used for numerical data that represent items like price, age, height, length, temperature and etc. Usually, in most programming languages numbers are separated into two subtypes like Integers and Float, Ruby has more subtypes of Numbers (Fixnum, Bignum, Float, Complex, Rational, BigDecimal).  

```
# We measure someones temperature.
# 'regular_temperature' variable is of type Number
regular_temperature = 36.6
my_temperature = 37.2
if my_temperature == regular_temperature
     print "You are not feeling well. Call you doctor."
else
     print "You are as healthy as a bear!"
```  
  
  
**Strings**: This data type is used for data that can be represented by a set of characters. These characters can be any character, like digits, letters, special characters and etc. Examples of this kind of data are name, surname, name of the city, password, code, phone number and etc. These data are defined within double quotes (“ ”) or single quotes (‘ ‘).

```
my_phone_number = "+993-65-201684"
print "My phone number is => " + my_phone_number
```  
  
  
**Boolean**: Being a data type that is present in almost all of the programming languages, this data type is primarily used to define only two available states. For instance: married/not married, open/closed, night/day, passed/failed and etc.  

In general, the Boolean data type uses only two available states that define truth/lie, to be exact “true” and “false” values. In some cases, number value 1 can be used instead of “true”, and 0, instead of “false”.  
```
exam_passed = true
if exam_passed
    print "Congratulations!"
else
    print "Do not loose your hope, keep trying..."
end
```  
Besides these, Ruby has a type “nil” that is used to represent data that has no value.  
  
  
**Arrays**: In this data type we can gather many items in one place. Arrays are a collection of items that are ordered in a certain order and each item has an index, that is, an order number. Ordering number of the items start with 0 For instance :
* Football players: Ronaldinho, Ibrahimovic, Figo, Carlos. (Indices of players, 0-Ronaldinho, 1-Ibrahimovic, 2-Figo and 3-Carlos)

```
players = ["Ronaldinho", "Ibrahimovic", "Figo", "Carlos"]
print players[0] # Player number 1 (index: 0), 'Ronaldinho'
print players[2] # Player number 3 (index: 2), 'Figo'
players[2] = "Rooney" # We replace 'Figo' with 'Rooney'.
print players[2] # Player number 3 (index: 2), 'Rooney'
```  
  
  

**Hashes**: This data type is very similar to the “Array” data type but elements are not ordered by an index or ordering number. Instead, each item in a collection is a pair of key and a value. This collection of items are defined within brackets “{ }” and each key and a value pair are defined using the “=>” (rocket) symbol. For instance:
* Color codes: red-0xf00, green-0x0f0, blue-0x00f

```
# Color names and codes
colors = {"red" => "0xf00", "green" => "0x0f0", "blue" => "0x00f"}
# 'puts' writes data to screen and ends the line...
puts "Red color code = " + colors["red"]
puts "Blue color code = " + colors["blue"]
```  

**Symbols**: Symbols are a bit weird data types that can not be found in every programming language. Described simply, this is a kind of “String” data type, but a bit improved version of the “String” data type. To define a “Symbol” data type it has to be one single uninterrupted word with a “:” in front of it. Let’s look at the examples below to differentiate “String” and “Symbol” types clearer.  

```
puts "a string".object_id # Output: 70358630335100
puts "a string".object_id # Output: 70358640625960
puts "a string".object_id # Output: 70358644379620
```

As you see in the example above, for the string with the same content, a new block of memory is occupied in a different part of computer memory. It can be noticed by looking at the ids (object_id) of the newly created objects. Now, let’s try the same thing with the “Symbol” data type.  
```
:this_is_string.object_id # Output: 1086748
:this_is_string.object_id # Output: 1086748
:this_is_string.object_id # Output: 1086748
```  

As you see in the example above, for the Symbol object with the same name/content, the same block of memory is used repeatedly. In other words, the second and third Symbol objects are not recreated. Instead, they use the memory block already created in the first Symbol object. Because the content is the same in all three. It saves us computer memory space and avoids recreating the object with the same content multiple times.  
  
“Symbol” is a bit more technical data type. So, just do not dive too deep into details right now. You will read more about it when you advance further in Ruby programming language.  
  
  
# Conclusion  
In this article, we got acquainted with Ruby programming language and look at its primary data types.  

If you want, you can try out the codes written above on this website: https://try.ruby-lang.org/. On that page, if the code you inserted into the “Editor” field works correctly, you will see the results in the “Output” section of the page.  

In writing this article, I tried to follow a clear and simple manner of writing. But if you have further comments and questions, you can proudly let me know about them, writing in the comments or emailing me directly.  

Hoping it will be useful to readers, learners, and enthusiasts…