DEV Community

Cover image for Ruby 101 part:1
Saima Rahman
Saima Rahman

Posted on

 

Ruby 101 part:1

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:

  1. Numbers (Integer & Floats)
  2. Strings
  3. Symbols
  4. 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.

Ruby Things!

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.

.even?

13.even? #=> false
12.even? #=> true

.odd?

14.odd? #=> false
15.odd? #=> true

Keep an eye out for my part:2!
Keep coding! 😁

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.