Ruby is a losing type language means you don’t need to mention the data type. It is easy to use and memorize. There are 4 basic types in Ruby they are:

  1. Number
  2. Strings
  3. Symbols
  4. Booleans

1. Number (Integer and Float)

# Integer
# Addition
1 + 1   #=> 2

# Subtraction
2 - 1   #=> 1

# Multiplication
2 * 2   #=> 4

# Division
10 / 5  #=> 2

# Exponent
2 ** 2  #=> 4
3 ** 4  #=> 81

# Modulus (find the remainder of division)
8 % 2   #=> 0  (8 / 2 = 4; no remainder)
10 % 4  #=> 2  (10 / 4 = 2 with a remainder of 2)
17 / 5  #=> 3, note that it is not 3.4

# Floats
17 / 5.0 #=> 3.4 

# Converting interger to float and float to integer
19.to_f #=> 19.0
19.0.to_i #=> 19

# Some useful number methods
# even and odd
200.even? #=> true
201.even? #=> false

199.odd? #=> true
200.odd? #=> false

Some useful number methods even and odd

200.even? #=> true
201.even? #=> false

199.odd? #=> true
200.odd? #=> false

2. Strings

In Ruby we define string into the single quote or double quote. There are many useful string methods are available in Ruby.

# assign a string
str = "I am string"
str.class #=> String

Some useful string methods Concatenation

str = "frist string"
str2 = "second string"
str.concat(str2) #=> frist stringsecond string
# there are some others way
p str + str2 #=> frist stringsecond string
p str << str2 #=> frist stringsecond string

Accessing String as like array elements

str = "Ruby on Rails"
p str[0] #=> "R"

"Ruby on Rails"[0]      #=> "R"
"Ruby on Rails"[0..1]   #=> "Ru"
"Ruby on Rails"[0, 4]   #=> "Ruby"
"Ruby on Rails"[-1]   #=> "s"

capitalize

"ruby on rails".capitalize #=> "Ruby On Rails"

include?

"ruby on rails".include?("on")  #=> true
"ruby on rails".include?("z")   #=> false

upcase

"ruby".upcase  #=> "RUBY"

downcase

"RUBY".downcase  #=> "ruby"

empty?

"ruby".empty?  #=> false

length

"ruby".length  #=> 4

reverse

"ruby".reverse  #=> "ybur"

split

"Ruby on Rails".split  #=> ["Ruby", "on", "Rails"]
"hello".split("")    #=> ["h", "e", "l", "l", "o"]

strip

" hello, world   ".strip  #=> "hello, world"

sub, gsub, insert, delete, prepend

"he77o".sub("7", "l")           #=> "hel7o"

"he77o".gsub("7", "l")          #=> "hello"

"hello".insert(-1, " dude")     #=> "hello dude"

"hello world".delete("l")       #=> "heo word"

"!".prepend("hello, ", "world") #=> "hello, world!"

3. Symbols

Strings can be changed, so every time string is used, Ruby has to store it in memory even if existing string. Here Symbol is useful. Symbols are stored in memory only once, so the operation done more faster. The application of Symbol is where string is not changed frequently like keys in hashes. Creating a Symbol

:it_is_symbol

String vs Symbol We can check the Strings and symbols by object_id.

"str" == "str" #=> true
"str".object_id == "str".object_id #=> false
":it_is_symbol".object_id == ":it_is_symbol".object_id #=> true

4. Booleans There are three type booleans in Ruby. true, false, nil True and False true and false represents that a expression is true or false. nil In Ruby, nil represents “nothing”. When a piece of code in ruby doesn’t have anything to return, it will return nil.