Hello, Rubians! This blog post is for newbies who want to learn the basics of Ruby or just here to refresh their memory!
What is Ruby?
Ruby is an object-oriented language. It simply means everything in ruby is an object, including the data types.
Four Basic Data Types:
- Numbers (Integer & Floats)
- Boolean (True, False, Nil)
Today I going cover Numbers only.
#Addition 2 + 2 #=> 4 #Subtraction 3 - 1 #=> 2 #Multiplication 3 * 3 #=> 9 #Division 20 / 5 #=> 4 #Exponent 2 ** 2 #=> 4 4 ** 3 #=> 64 # Modulus (find the remainder of division) print(4 % 2 ) #=> 0 (4 / 2 = 4; no remainder) print(15 % 4) #=> 2 (10 / 4 = 3 with a remainder of 3)
Difference between Integers and Floats
Integers are whole numbers, such as 16 and floats are decimal numbers such as 16.0, 0.25, or 4.3.
In Ruby, when you want to do some arithmetic with two integers the answer will always be a whole number:
5/2 #=> 2
It is weird, right? In order to get the correct answer, we have to use float.
5/2.0 #=> 2.5
Convert Integers to floats or vice versa
Integer to a float: 2.to_f #=> 2.0 18.to_f #=> 18.0 Float to an integer 3.0.to_i #=> 3 1.8.to_i #=> 1
Number methods in Ruby:
Well, there are many numbers in Ruby, I am going to share two over here, rest you can check here.
13.even? #=> false 12.even? #=> true
14.odd? #=> false 15.odd? #=> true
Keep an eye out for my part:2!
Keep coding! 😁
Top comments (0)