Data Types

As the name suggests, data-type specifies the type of the data. There are different built-in datatypes in Ruby:

1. Numbers

Numbers represents both Integers and floating point numbers.

ClassDescriptionExample
FixnumRepresents small integers99
BignumRepresents big intergers999999999
FloatRepresents decimal numbers7.9
ComplexRepresents imaginary numbers5 + 2i
RationalRepresents fractional numbers10/3
BigDecimalRepresents Precision decimal numbers79.2

2. Boolean

Boolean data type represents either true or false.

3. Strings

String represents a series of characters. Strings can be enclosed with in either single quotes, or double quotes.

str1 = "hello World";
str2 = 'good morning';

4. Hashes

Hashes represents key-value pairs. => is used to assign value to it's key.

hsh = nationalGame = { "Australia" => "Cricket", "Japan" => "Wrestling", "NewZealand" => "Rugby","USA" => "Baseball"}

5. Arrays

Array is a collection of data items. They don't need to be of same type. It can contain all different types of data.

arr = [ "Good", "morning", 9, 5.32 , true]

6. Symbols

Symbols are used instead of strings because they are light-weight strings and occupy less memory. A symbol is preceded by a colon (:).

:oc => "OneCompiler"