Ruby 101: The Basics
Part I: Variables, Control Flow and Looping
Introduction / History
Ruby is a “dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.” created by Matz.
He has often said that he is “trying to make Ruby natural, not simple” in a way that mirrors life.
Ruby is simple in appearance, but is very complex inside, just like our human body — Matz
For me the first reason to learn Ruby was that it is, in fact, a beautiful programming language. It is really natural to code it and always express my thoughts.
The second main reason was Rails. The well known framework that Twitter, Basecamp, Airbnb, Github, and so many companies use.
The Basics
1. Variables
You can think about variables as a word that stores a value. Simple as that.
In Ruby it is really easy to define a variable and set a value to it. Imagine you want to store number 1 in a variable called “one”. Let’s do it!
Wow! How simple was that? You just assigned the value 1 to “one” variable
And you can assign some value for whatever variable you want. “two” variable stores 2 integer and “some_number” stores 10000.
Besides integers, we can also use booleans (true / false), strings, symbols, float and so many other data types.
2. Control Flow: conditional statements
“If” uses an expression to evaluate to true or false. If it is true, it executes what it is inside the if statement. For example:
2 is greater than 1, so the “puts” code is executed.
The “else” statement will be executed if “if” expression is false.
1 is not greater than 2, so the code inside “else” will be executed.
You can also use “elsif” statement. Like that:
One way I really really like to write ruby is using “if” statement after the code to be executed:
It is so beautiful and natural as Matz says. It is idiomatic Ruby.
3. Looping / Iterator
In Ruby we can iterate in some many different forms. I’ll talk about 3: while, for and each iterator.
While Looping: while the statement is true, the code inside the block will be executed. So this code will print the number from 1 to 10.
For Looping: you pass the variable “num” to the block and the “for” statement will iterate it for you. This code will print the same as “while” code: from 1 to 10.
Each Iterator: I really like “each” iterator. It is simple and expressive. For an array of values, it’ll iterate 1 by 1, passing the variable to the block.
You are maybe asking what is the difference of “each” iterator and so well known “for” looping. The main difference is that “each” iterator maintain the variable only inside of scope. “for” looping keeps the variable live outside the block. Let’s do a test:
The variable “num” in the “for” looping will keep alive after iteration block. “num” variable from “each” iterator block will not.
That’s it!
We learnt a lot of things about Ruby basics:
- How Ruby variables work
- How Ruby conditional statements work
- How Ruby looping & iterators work
And that’s it guys! I want to update this article. The idea is to share a great content and the community helps improve it!
I hope you guys can appreciate the content and learn more about how Ruby works. And for the second part of Beautiful Ruby series, we are gonna learn about Ruby Data Structures.
If you want a complete course, learn real-world coding skills and build projects, try One Month Ruby Bootcamp. See you there ☺
Have fun, keep learning & always coding!