Ruby Basics 101 - part one
Tags: ruby
Published: 2022-08-25 13:54:00

## Introduction


Hello from the mines, I am Peter and I will be your guide as we go through the tunnels and discover how to mine and purify gems. In that order Ruby to be specific. Well depending on who you ask, Ruby can be a precious stone  or a tool to the dark web: hahah just kidding.  Before jumping into the tunnels let us understand and see how ruby came into existance. 

In the early 90's there was this mage chosen by the inhibitants of earth 10 to come up with a way they could communicate with robots to reduce chances of a skynet happening. The mage was given the code name **matz** while his ID was Yukihiro Matsumoto. **Yukihiro matsumoto** created **Ruby as a general purpose language** for the inhibitants of earth 10 also refffered to as programmers/developers/coders. 

Let us Begin!!!!!!!!!!!!!!!!!!!!!!!!!!

![Runner on set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiz8z6d0xb1llhywmzfv.png)
 

Before we get to see how to identify  ruby  stones, we need tools that will allow us to be as efficient as posssible. Well you dont have to worry where to find the aforementioned tools, just dig below
- [Visual studio code](https://code.visualstudio.com/) 
- [RVM - ruby version manager](https://rvm.io/rvm/install)

Well getting to read the shovel manual: RVM docs  can be challenging sometimes, well you dont have to worry as we have compressed the instructions below
```
# As a first step we get keys to verify our installation
$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

# make sure to have curl installed before running the command below
$ curl -sSL https://get.rvm.io | bash -s stable --ruby

curl -sSL https://get.rvm.io | bash -s -- --ignore-dotfiles

# check if rvm is intsalled
$ rvm -v

# install ruby using rvm
$ rvm install 2.7

# check if ruby is installed
$ ruby -V

```

With the above  instructions we are ready to find and explore the properties of the ruby stones in the mine. Phweks breath, that was some work at the mine right?
Now that we have  all the tools wee need let us dive into mining the rubies. Allow me to change my language setting to speak like an inhibitant of earth10.

`Language  loading ...........................Language  loading ...........................Language  loading ...........................Language  loading ...........................Lnguage loading............................................Done
`
Before we can do anything in ruby, we will need to have it installed on our machines which we did up top as we were setting up our tools. To confirm ruby is installed just re run the command up top 
```
$ ruby -v

# should return output 
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a)
```


### Data Types

Just as we humans have structure to our language so do programming langauges or in this case so does the language of mages Ruby. This structure allows us to communicate well with our machines without having to leave behind what is natural to us as earth10 inhibitants. This structure states data should be categorized  as follows 
```
1. Numbers
2. Strings
3. Hashes
4. Symbols
5. Boolean
6. Arrays
```
For us to understand how this structure will guide let us jump into every single one of them and understand how to work with them.


#### Strings

This is a data type that is usually a sequence of characters enclosed in quotation marks prefferably double. we can have examples below
```
"John Doe"
"Joe Tribiani"
"Racheal Geller"
```
We can go  on and on but you get the gist of things.


#### Numbers

This data type is numerical reprisantaion of values, excels in quantifying things, people or ideas. Well you might think a number is a number but in the eyes of ruby a number is seen as two parts i.e Whole number and a decimal number popularly known as a float. examples are below
```
10 -> whole number
10.5 -> Float number
```


#### Boolean

This data type stores or denotes truthy or falsy values, which can be assigned explicitly or as a result of a condition. 
**NB: Something to note in ruby is all values are truthy values except false and nil:we will see what nil is all about**
```
true 

10 > 1 # greater than comparison operator evaluates to true as 10 is greater than one

false
```

Before jumping to the next data types, let us take a pause to cover something important. Looking at booleans, numbers and strings there  is no way to refer back to the values without having to remember them explicity. Well thats not efficient. How then can we make this efficent and easy for us?

Variables is the big word that will make it for us, but what are variables. Variables are named containers that store our values , using the name makes it easy to know what the value is all about without having to remember the actuall value.  To drive the point home, how likely are you to remember a veicles chasis number as compaired to the vehicle model.

so how do we get to utilise variables, dont check anywhere else other than in the code snippet below
```
# we use the equal operator to assign the value on the right to the variable name on the left

name = "john" 
age = 10                                               
```


#### Array

This data type allows you to group or store values sequntially seperating the individual items using the comma, mostly its recommended to store data of the same type in an array. The items are enclosed in square brackets
```
# An array with numbers
[10, 20, 10]

# An array with strings
["Monicah", "Racheal"]

# mixed array
[10, "Hello world", false]
```
When you observe an array, a question that may arise is how do you access individual elements. well we get to use a principle called indexing, same as a library uses index to find books so will we but ours will be a bit different. For us to access an element in an array we will use the bracket-notetion to do the indexing.
**NB: Indexing begins at zero in array and strings**
```
# names array
names = ["joe", "john", "jan"]

#accessing the first element
names[0] => rreturns "joe"

```



#### Hashes

If clearly take a look at arrays you get to notice that there is no context to the data even when accessing it. Worry not as where arrays fail that is where hashes excel as they store their values in a key, value pair, what does this mean you might ask. If you coming from python hashes follows to close principle, how dictionaries behave but if you comming from javascript the closest you can get this will be an object.
**NB: Hashes have their keys being as individual data type i.e symbols**
```
# creating a hash using key symbol
{name: "john"} 

#creating key using symbol with rocket syntax
{name => "john"}
```



## Conclusion

For this first part we covered what ruby data types are, in the next part we will cover a more advanced topics
- Control Flows
- Looping
- Methods
- OOP