Get Started With Ruby
Tags: ruby, beginners, tutorial, programming
Published: 2022-04-14 18:10:27

## What is Ruby?
Ruby is a dynamically typed, interpreted, object oriented programming language. Ruby was created by Yukihiro 'Matz' Matsumoto in Japan in the 1990's. Ruby has a very minimal syntax making it very readable and easy to write. If you're already familiar with another object oriented language like Python, learning Ruby will probably come very quickly. Ruby along with the Rails framework can allow you to build back ends very quickly. For a deeper dive on Ruby you can check out [this](https://www.ruby-lang.org/en/about/) link. For more information of Rails you should check out [this](https://guides.rubyonrails.org/) link.


## Installing Ruby
To install ruby head over to [the ruby-lang site](https://www.ruby-lang.org/en/documentation/installation/) and follow the instructions for your operating system. I use a Mac and Homebrew so all I had to type in my terminal was `brew install ruby`. Windows and Linux will have slightly different ways to install so again just follow the instructions linked above and you should be fine. After installing ruby you should run the command `ruby -v` to check to make sure everything installed properly. If you use VS Code you can install [this](https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby) extension to help with things like syntax highlighting, as well as auto complete and formatter support.


## Using IRB
Ruby comes with its own REPL that can be used in the terminal called IRB. If you have ever used Node.js, IRB is pretty similar. To use IRB just type `irb` into your terminal after installing ruby, you can then run Ruby code. You can test this out by typing `puts 'Hello, world!'` into an IRB session, you'll see that this returns `nil` after printing 'Hello, world!'. 


## Using Ruby
To give an example of how you can use Ruby, I'll show you how to solve Fizz Buzz. While this won't be an example of how to write object oriented code, it will give a very brief overview of syntax. If you're not familiar with Fizz Buzz you can check out the leet code question [here](https://leetcode.com/problems/fizz-buzz/). Ruby has a pretty easy to read syntax so if you have experience with a similar language you'll probably be able to follow along fairly easily but if not I'll include comments to explain what's going on. <br />
First, lets create a new directory by typing `mkdir ruby-fizzbuzz` into the terminal. Next you can move into the new directory by typing `cd ruby-fizzbuzz` into the terminal as well. Now that you're in the new directory run the command `touch fizzbuzz.rb`, to create a new ruby file. After creating the file you're good to open the directory in whatever code editor you use.
<br />
First, let's define a fizz_bizz method.

``` Ruby
#Use def to define a method, you don't need ( ) around parameters.
def fizz_bizz n
#Always remember to 'end' your methods.
end
```
Next, lets create an empty solution array.
``` Ruby
def fizz_buzz n

    answer = []

end
```
We'll also need to set up a loop to iterate through a range of numbers. There are a ton of ways to loop in Ruby. I did a whole post on looping in Ruby that you can read [here](https://dev.to/kadeesterline/looping-in-ruby-351b). [This](https://www.rubyguides.com/ruby-tutorial/loops/) is also a pretty good resouce for more information on different types of loops in Ruby.
``` Ruby
def fizz_buzz n

    answer = []
    #this is a very simple way to loop in ruby
    n.times do |i|
    #remember to always 'end' loops as well!
    end

end
```
Now all we need to do is add some conditional logic to check each value in the range given
```Ruby
def fizz_buzz n

    answer = []
    
    n.times do |i|
        #check if value is divisible by 15
        if (i + 1) % 15 == 0
            #shovel in 'FizzBuzz'
            answer << 'FizzBuzz'
        #check if value is divisible by 3
        elsif (i + 1) % 3 == 0
            #shovel in 'Fizz'
            answer << 'Fizz'
        #check if value is divisible by 5
        elsif (i + 1) % 5 == 0
            #shovel in 'Buzz'
            answer << 'Buzz'
        else
            #if none of the above is true, shovel in value as a string
            answer << "#{i + 1}"
        #always 'end' conditional statements
        end
    end
    #print array to terminal
    puts answer
end
#call the function
fizz_buzz 100
```
And that's really all you have to do! This solution could still be refactored but is probably the first solution a lot of people would come up with when approaching the problem with Ruby. To Run the code just make sure you're in the right directory and run `ruby fizzbuzz.rb`. To Check it out in a REPL you can check out [this] link.

## Uses for Ruby
You'll hear all over the internet that Ruby is dying or that it is already dead. While Ruby is no longer being used as much as other languages it is still being used. There might not be as many jobs for Ruby in the market that you live in though, and you should probably make sure it's the right fit before diving into learning it. The [Stack overflow developer survey](https://insights.stackoverflow.com/survey/2021#most-popular-technologies-language-prof) is a good resource to see what is actually being used, but you should probably leverage things like indeed, glassdoor and other job posting sites to check for your local market. There are companies that still use Ruby in 2022 like twitter, shopify, hulu and many more. You'll want to pick up the Rails framework which allows for very fast fullstack app and api development. 

## Resources
Here are a few more resources that may come in handy as you learn more about Ruby:
- [Ruby in 100 seconds from Fireship.io](https://www.youtube.com/watch?v=UYm0kfnRTJk&ab_channel=Fireship)
- [Ruby docs](https://ruby-doc.org/)
- [Rails docs](https://guides.rubyonrails.org/)
- [My other posts](https://dev.to/kadeesterline)
- [Great resource with lessons](https://exercism.org/tracks/ruby)