Day 2: Basics of ruby
We are continuing our journey of learning ruby and today we are going to learn the following:
- Basic data types
- Single and Multi line Comments
- Using methods
- Basic math operations
- Fun with Strings
Setup for Today
Create a new folder called Day_2, then create a new file with the name basics.rb and finally open it up in a plain text editor of your choice.
cd 100_days_of_ruby/
mkdir Day_2 && cd Day_2/ && touch basics.rb
codium .
Basic Data Types
Type the following to your text editor:
# Assigning values to variables
apples = 4 # Integer
hello = "Hello" # String
money = 6.5 # Float
is_adult = true # Boolean
In ruby you don't have to define the type of a variable before being able to assign a value to it. For example in C++ you would say int apples = 4
in ruby though you can simply assign any value to any variable that you want.
Ruby as you can see has all the basic data types that you see in other languages too. We can have Integers, Strings, Floats and Booleans.
Single and Multi line Comments
If you noticed the first line of our file starts with #
, in ruby this is how you create a comment.
Comments are super useful for us developers that are going to look the code of an app. If you are building a new app and you want other people to contribute to your project, leaving comments to explain how something works is a very good habit to build. It will help others understand what you wrote but also it will help you too, if for example you haven't looked at a piece of code in a long time it can be hard to remember what exactly you were doing.
So comments are always useful and you should use them when you think that a piece of code needs some sort explanation.
There are two types of comments in ruby:
- Single line comments
# I'm a single line comment. Everything we type after the character # is a comment.
- Multi line comments
=begin
I'm a multi line comment.
We can type anywhere in between =begin and =end
And we can have as many lines as we want!
=end
We use single line comments when we want to give a sort explanation to a line of code. And we use multi line comments when we want to explain something in depth (don't over do it though).
Using methods
We won't go over what methods are in depth today but we will need them. So for now methods provide a useful functionality that you can call on an object. Don't worry about it too much and add the following lines of code to our program.
puts apples.is_a?(Integer)
puts apples.is_a?(String)
Now run the program by typing ruby basics.rb
. The output should be the following:
true
false
We have assigned apples = 4
so the variable apples
is an Integer. Now we called the method is_a?
to check what type is the variable apples
. Obviously when we say apples.is_a?(Integer)
it returns true since apples
has the value 4 and returns false when we check if it's a String.
Basic math operations
Add the following to our program:
# Basic math operations
x = 5
y = 12
puts x + y # Addition
puts x - y # Subtraction
puts x * y # Multiplication
puts y / x # Division
puts y % x # Modulo
puts x ** 2 # Exponent
These are very self explanatory so I won't go over them. What I wanna highlight though is that when we are calculating a Division or a Modulo operation we always want one of our variables to be of type Float.
Here x
and y
are both Integers so when do y / x
then answer will always be an Integer. Sometimes that's not a problem, for example if we had 6 / 3
that would be equal to 2
which is an Integer. But in our case we have 12 / 5
which is equal to ~2.4
.
In our output we can see that the result of y / x
is 2
which is wrong. To fix that we need to simply make either x
or y
a Float.
x = 5.0
y = 12
Run it again and you will notice that now the output is 2.4
as it should be.
Fun with Strings
As we know already a string is everything that's between " "
. But there are a lot of things that we can do with Strings, we can manipulate them in a lot of ways.
Add the following to our program:
# Fun with Strings
puts "Hello," + " " + "World!" # Concatenating strings
age = 22 # age is an Integer
puts "I'm #{age} years old!" # Interpolating
Let's break everything down. In the first line we are concatenating three strings together. It might be weird at first that we are using the +
plus symbol for concatenating strings but that's common with many languages.
Later we define a variable called age
and assign it the value 22
. But on the next line we did something weird, we used the #
symbol inside of a string.
This is how we do Interpolation in ruby, we basically do #{var}
and that will "embed" the value of a variable to a String.
We will end day #2 by just playing around with some String methods.
Add the following to our program:
puts hello.reverse # print the string in reverse order
puts hello.upcase # print the string with every letter being upercase
puts hello.downcase # print the string with every letter being lowercase
puts hello.length # print the length of the string
puts hello.empty? # check if the string is empty => returns false cause it's not the case
puts "".empty? # check if the string is empty => returns true cause we have an empty string
I have explained every method with a comment even though most of them are self explanatory.
Tomorrow we will go over OOP in ruby and learn a lot more about how ruby really works.
Thanks for joining, I will see you tomorrow!
Top comments (0)