Shine bright like a.. Ruby 💎
Tags: beginners, webdev, ruby, sinatra
Published: 2023-05-29 18:05:54

Hello and welcome to my coding journey! 

For those of you who have seen my previous posts, welcome back! I hope you enjoy and find this one useful. For those of you who just happen to stumble upon this post, hi! My name is Paul and I am currently a student with [Flatiron School](www.flatironschool.com) on a journey to learn the basics of software engineering. 

As you can tell by the title, this post will touch subjects as [Ruby](https://ruby-doc.org/core-2.7.3/), [gems](https://rubygems.org/) and a ✨sprinkle✨ of [SQLite](https://www.sqlite.org/index.html).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ublrv42hp3se61cb3ovw.png)[Source](https://www.devopsschool.com/blog/wp-content/uploads/2022/03/eb9e3b7dab09358e7cf13f188f64f9f4.png)

If you've followed along you know I have tapped into the basics of vanilla JavaScript and React. What are those useful for? They allow us to create the 'visuals' or the **client-side** of a web application. What does Ruby bring to the table? It helps us build the backend part of our application or the **server-side**. 

You may ask 'But Paul, how many languages are you supposed to learn in order to build websites?'. And that is a valid question! If it feels like a lot to learn, it's probably because it is. No lie. But, a good lesson we are thought is that a good developer knows how to use [Google](www.google.com) to his advantage. Learning a new language may sound scary at first, but it shouldn't be the first thought. If you did it once, you can do it again. 

## What is Ruby?  💎 
Ruby is a programming language that was created by [Yukihiro “Matz” Matsumoto](https://matz.rubyist.net/) in 1995. We can think of Ruby and mix of _Perl, Smalltalk, Eiffel, Ada, and Lisp_. His goal was trying to make Ruby feel natural, not simple.

At its core, Ruby is a dynamic object-oriented programming language and due to it's flexibility, we can build different programs (not just web applications) such as:

- Command line interfaces
- Web servers
- Games
- Web scrapers

Read more about [Ruby](https://rubystyle.guide/).

## How to write and run Ruby 

A different aspect of Ruby is that we won't be able to run the code in the browser as we used to with JS and React. We will be using our terminal. 

To get started we need to a create a new file and add the .rb extension. 
For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp4gqpfxnwght7af27sn.png)

That's right. Both work! It is up to us to choose if you want to use the parentheses or not. 
_However_, as an unwritten rule, most Ruby developers prefer the first syntax as it's more pleasant to look at 😊.

Now to run this code, we need to type `ruby test.rb` (or the directory you're in, for example, my file is in lib so therefor the command is ruby lib/test.rb)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt890gx97hvxx7dk28k1.png)


And voila! We just ran our first Ruby application!

## Data types in Ruby

Just like in JavaScript, we have the same types of data we work with:

- strings
- numbers
- booleans
- nil _(aka null in JS)_
- arrays
- hashes _(aka objects in JS)_

## Functions aka methods

In JavaScript we learned about functions: **lowercase** for vanilla Javascript and **PascalCase** for React. In Ruby we call them **methods**. And yes, we can either write our own or we can use the built in methods! (simply add `.methods` at the end of your method and see the full list of available methods you can use for your instance.
How cool is that?!

Example: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmfur89cawnppnq3n0h0.png)

And the results we're getting is -->


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm8gri6g3e9nzlq5twap.png)

In order to write a method, we will be writing the following: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmlfh7jxt1lxugvdgjxa.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43gkrbcqoemco4wz9cma.png)

## Classes and instances

In order to create a class, we simply type:

`class Test 
end`

What is a class? It's a the blueprint, template or 'a small factory' that creates more objects with the same characteristics. What method create more individual objects? `.new`

By using `.new ` we create new **instances** (with it's own unique ID).

_For this example we will be using a Ruby built in REPL called 'pry'. Add `require 'pry'` at the top of your file and then `binding.pry` at the end of your code. _

Example: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym350ph1jomj9hodipdl.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie7jry8ewvowz33xk69x.png)

How amazing is that?

## Gems 

As we discussed with React, we are not the first developers therefor there is no need to reinvent the wheel. Programmers before us have been kind enough to save code that we can use to make our lives easier in libraries. Ruby is no exception. We can use templates of code called **gems**. 

Read more about [gems](https://rubygems.org/).

## SQL

When we are trying to develop a web application, we usually use data to present it or we take the user's input and store that data. 

How can we store data? Well, if the answer begins with _data_ and ends with _bases_, then you got that right! **Databases**!

One aid we have in storing data is SQL. This won't be the tool we use to write a big complex application, but we can use it to interact with that database. 

SQL (_Structured Query Language_) - pronounced 'sequel' or 'S-Q-L' - is the language responsible for managing data in databases. The con is for that we use it for _only_ talking to the database.

**PRO TIP**: If your laptop is on OSX version 10.4 or above, chances are you already have SQLite installed. To double check, run the command `which sqlite3` and if you get positive response, you are good to go! Thanks [Apple](www.apple.com)!

The database files are ending in .db extension and you can open them in your terminal by running `filename.db sqlite3`.

A few of the SQL commands are:

- CREATE TABLE
- ALTER TABLE
- DROP TABLE

For example, if we want to create a table we would do the following: 

1. run `sqlite 3 filename.db`
2. `CREATE TABLE table_name ( id INTEGER **PRIMARY KEY***, name STRING, age INTEGER);`

And voila! We have created a SQLite table. 

*****Every table we create must include a primary key to be indexed, just like an Excel Spreadsheet. 

Read more about [SQLite](https://www.sqlite.org/lang_keywords.html).


## Final thoughts

There is so much more to read and learn about this! In this post, I am barely scratching the surface, but I hope it will spark your interest to dive deeper into it and discover the wonders of Ruby, gems and SQLite!

Until next time, **happy coding**!