Home » Ruby Data Types

Ruby Data types

Data types represents a type of data such as text, string, numbers, etc. There are different data types in Ruby:

  • Numbers
  • Strings
  • Symbols
  • Hashes
  • Arrays
  • Booleans


Integers and floating point numbers come in the category of numbers.

Integers are held internally in binary form. Integer numbers are numbers without a fraction. According to their size, there are two types of integers. One is Bignum and other is Fixnum.

FixnumThey are normal numbers1
BignumThey are big numbers111111111111
FloatDecimal numbers3.0
ComplexImaginary numbers4 + 3i
RationalThey are fractional numbers9/4
BigDecimalPrecision decimal numbers6.0


  • In a calculation if integers are used, then only integers will be returned back.
  • Ruby Data types 1

  • In a calculation if float type is used, then only float will be returned back.
  • Ruby Data types 2

  • In case of dvision, following output will appear.
  • Ruby Data types 3


A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within single (‘) or double (“) quote.


  • Two strings can be concatenated using + sign in between them.
  • Ruby Data types 4

  • Multiplying a number string with a number will repeat the string as many times.
  • Ruby Data types 5


Symbols are like strings. A symbol is preceded by a colon (:). For example,

They do not contain spaces. Symbols containing multiple words are written with (_). One difference between string and symbol is that, if text is a data then it is a string but if it is a code it is a symbol.

Symbols are unique identifiers and represent static values, while string represent values that change.


Ruby Data types 6

In the above snapshot, two different object_id is created for string but for symbol same object_id is created.


A hash assign its values to its keys. They can be looked up by their keys. Value to a key is assigned by => sign. A key/value pair is separated with a comma between them and all the pairs are enclosed within curly braces. For example,

{“Akash” => “Physics”, “Ankit” => “Chemistry”, “Aman” => “Maths”}



Ruby Data types 7


An array stroes data or list of data. It can contain all types of data. Data in an array are separated by comma in between them and are enclosed by square bracket. For example,

Elements from an array are retrieved by their position. The position of elements in an array starts with 0.



Ruby Data types 8

Next TopicRuby if else

You may also like