100 Days of Learning ruby - Day #1

Introduction

Hi, I wanted to learn ruby and what better way than taking up the challenge of #100daysofcode. To be honest I'm not totally new to ruby, but I'm by no means an expert.

The plan is to code in ruby everyday for at least 1 hour. At the end of the session I will create a new blog here highlighting everything new I learnt in a tutorial style. This way I'm teaching you what I learnt by explaining every detail, this will only benefit me by forcing me to actually understand what I'm doing instead of just typing things.

I think that these blog posts will also help you learn ruby with me, so I welcome everyone who wants to learn ruby.

Note: I probably won't be covering Ruby on Rails or any other framework. People often mistake ruby with Ruby on Rails, so I wanted to make this disclaimer.

With all of that out of the way, let's start learning!

Day 1: Up and Running

The first thing we need to do is to install ruby and installing it can be a bit tricky some times. Depending on your operating system the process of installing ruby might be different.

  • Windows

    To install ruby on a Windows machine it's actually pretty easy thanks to RubyInstaller. I won't go over how to install ruby using RubyInstaller cause it's really easy, just follow the steps of the installer and you will be good to go.

  • Gnu/Linux

    Installing ruby on a Gnu/Linux machine is a bit harder depending on how much comfortable you are with the terminal of course. As far as I know there is not a one click installer for ruby as RubyInstaller for Windows. On Gnu/Linux you generally have two options:

    • Package Manager

      Using the package manager of your distribution is generally not advised cause most of the time it doesn't have an up to date version of ruby. So I would avoid using this option even though it's easier.

    • Using a Version Manager

      This option is generally preferred cause not only you can install the latest version of ruby but you can also manage multiple versions of ruby in your system. There are a lot of options and I will highlight the most used ones. We can use rvm (ruby version manager), rbenv with ruby-build and finally ruby-install with chruby.

Installing rvm

I'm going to use rvm for installing and managing ruby on my machine. I'm not going to go over how to install rvm since they have a really nice guide over on their website.

Just follow the instructions here and you will have rmv installed on your system in no time.

Check if you have rvm installed correctly by typing the following on your terminal:

rvm --version

You should have something similar to this:

rvm 1.29.12-next (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

Installing ruby

Now that we have rvm we can easily install any version of ruby with one command. You can find all of this info on their page here.
Type the following command to install ruby 3.0.2 on your system:

rvm install 3.0.2

After this is done go ahead and use that version:

rvm use 3.0.2

Note: I have already installed ruby 3.0.2 on my system and currently the latest version of ruby is 3.1.0 and this won't affect my 100 days of code but if you want the latest version of ruby go with 3.1 instead.

Now if we did everything correctly we should have ruby 3.0.2 installed on our system, to check this type the following:

ruby --version

If you see the following then you have correctly installed ruby and we are ready to begin learning.

ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]

Hello, World!

We will end day #1 of learning ruby by creating our very first ruby program. As everyone knows your first program when learning a new programming language is always the infamous Hello, World! program.

So let's begin by creating a new folder called Day_1 and then create a new ruby file with the name hello.rb.

cd 100_days_of_ruby/
mkdir Day_1 && cd Day_1/ && touch hello.rb

Now open hello.rb in a plain text editor of your choice. Type the following and save the file:

puts "Hello, World!"

To run this simple ruby program open up a terminal and type the following command:

ruby hello.rb

You will see the output Hello, World! in your terminal.

Explanation

This is a very simple ruby program with not much going. We are basically printing Hello, World! on our screen.

We do that by using the keyword puts, which prints a string of characters and also goes to the next line. We provided a string of characters by typing Hello, World! inside of " ". Finally to run the program we typed ruby hello.rb. Basically when we want to run a ruby program we just type ruby and the path to our .rb file.

Thanks for joining, I will see you tomorrow!

13