DEV Community

Paul Bosorogan
Paul Bosorogan

Posted on

Shine bright like a.. Ruby 💎

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 on a journey to learn the basics of software engineering.

As you can tell by the title, this post will touch subjects as Ruby, gems and a ✨sprinkle✨ of SQLite.

Image descriptionSource

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 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 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.

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

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

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

And the results we're getting is -->

Image description

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

Image description

Image description

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

Image description

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.

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!

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.

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!

Top comments (0)