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:
Number
Strings
Symbols
Booleans
1. Number (Integer and Float)
Some useful number methodseven and odd
2. Strings
In Ruby we define string into the single quote or double quote. There are many useful string methods are available in Ruby.
Some useful string methodsConcatenation
Accessing String as like array elements
capitalize
include?
upcase
downcase
empty?
length
reverse
split
strip
sub, gsub, insert, delete, prepend
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
String vs Symbol
We can check the Strings and symbols by object_id.
4. Booleans
There are three type booleans in Ruby. true, false, nilTrue 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.