Introduction to Ruby and Ruby on Rails
So today we are going to start with a new tutorial on Ruby. Basically, Ruby is a programming language. Ruby has been used a lot in the programming world and it is one of the most important languages. We are going to start with some basics of Ruby and then we are going to move on to a framework on Ruby that is Rails, properly known as Ruby on Rails. Initially we'll start with the installation part and then we are going to proceed and enhance ourselves to the Ruby on Rails framework.
Ruby vs Rails and Installation Overview
To be honest, Ruby is a programming language and Ruby on Rails is a framework based on the Ruby language which is used in web development, basically as a backend framework. You can create your own backend framework and then you can extend it to the front end as well using ERBs. First we'll start with the basics of Ruby and then continue. To install Ruby on your machine you can move to this website, Ruby Installer for Windows. You can download by clicking the download button and you'll have all the versions. Make sure you install one with the DevKit as it will help you. Choose depending on your requirement like 32-bit and 64-bit. I had downloaded the one at the top. When you download you'll get an installer which will take some time to install. It's a very easy process; it will keep on installing. When you install this installer then you can move ahead. Another website you can refer to is ruby.org. These are the two documentations you can refer to.
Preparing to Run Ruby Programs in VS Code
Let's move on to the tutorial. After moving to the installation and documentation you can start running some Ruby programs on your machine. Initially we'll start with them. I'll show you how you can create one and run one. This is my VS Code editor. I have created a folder named ruby. Ruby files use the .rb extension. Create a file named hello.rb. Now I'm inside this folder which has a hello.rb file. To make sure you have installed Ruby on your machine you can type ruby -v. Ruby -v will give you the version you have installed and it's perfectly fine.
Hello World and Running Ruby Scripts
First we are going to create a Hello World Ruby program. I'll clear the terminal. Now we are going to do puts "hello world". This would create the Ruby program. To run the Ruby program what you need to do is ruby hello.rb. This will run the program on your machine and it gives the output "hello world."
Variables and String Interpolation
We will start with some basics like creating variables. a = 1, b = 2. I'll put c = a + b and then puts c. First I'll run this and show you how this works; it works perfectly fine. If you want to do string formatting you can put a string like "the sum of #{a} + #{b} is equal to #{c}". Using the hash symbol with curly braces gets the value of that particular variable and formats it inside the string. Now running this shows "The sum of 1 plus 2 is equal to 3." This is how we achieve it.
Output Methods: puts vs print
There is another statement in Ruby which is print. print "hello world". Running this prints "hello world". What's the difference between puts and print? I'll show with examples. puts "first line" and puts "second line" gives output on separate lines. Using print "first line" and print "second line" does not give a line break. To achieve a line break with print you can add a "\n". Now the output matches puts. So puts always gives a newline automatically, but print does not. That's the only difference between puts and print.
Defining Methods, Return Values, and Multiple Assignment
Before we move to conditions, let's make a simple function. Define add(a, b). Since this is Ruby you don't need a colon like in Python. End the function with end. The body can either return a + b or puts a + b. For simplicity, store c = a + b and then puts c. Call add(2, 3) and add(2, 7) and it works fine. We can also return a + b and capture the return value: result = add(a, b); puts result. This is how return works in Ruby. You can also swap in one line: a = 5; b = 10; puts a, b. Then a, b = b, a. After running, 5, 10 is replaced with 10, 5. Multiple assignment like x, y, z = 1, 2, 3 is valid. Print x, print y, print z. This is a valid statement in Ruby. We can assign values using comma separation. Thank you everyone for watching this tutorial. I'll catch you in the next video. Thank you.