Ruby Tutorial Ruby References

Ruby - Data Types



Data types in Ruby represents different types of data like text, string, numbers, etc. All data types are based on classes as Ruby is a pure Object-Oriented language. There are different data types in Ruby which are mentioned below:

  • Numbers
  • Strings
  • Boolean
  • Hashes
  • Arrays
  • Symbols

Ruby Numbers

Numbers are simply a series of digits. Negative numbers are created by prepending a minus sign. Dot (.) is used to create decimal numbers. An underscore can be used to separate thousands places, for example 1_234.56 is same as 1234.56. However, this is optional.

Numeric class is the parent for all the number classes. All number classes are mentioned below:

ClassDescriptionExample
IntegerParent class of Fixnum & Bignum1
FixnumWhole numbers that fit into the OS integer type (32 or 64 bits)1
BignumUsed for bigger numbers111111111111
FloatImprecise decimal numbers10.5
ComplexUsed for math stuff with imaginary numbers(1+2i)
RationalUsed to represent fractions(5/3)
BigDecimalPerfect precision decimal numbers3.0

Example:

The example below illustrates the Numbers data types in Ruby.

#Integer - Fixnum and Bignum types
puts "A Fixnum type : #{10}"
puts "A BigNum type : #{123456789000}"

#Float type
puts "A Float type : #{10.5}"

#Complex type
puts "A Complex type : #{1 + 3i}"

The output of the above code will be:

A Fixnum type : 10
A BigNum type : 123456789000
A Float type : 10.5
A Complex type : 1+3i

Ruby Strings

Strings are simply a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within single (') or double (") quote. Double-quoted strings allow substitution and backslash notation whereas single-quoted strings doesn't allow substitution and allow backslash notation only for \\ and \'.

Example:

The example below illustrates the Strings data types in Ruby.

x = "this"

puts "Ruby String Data Type."
puts "Double quoted string allow substitution like #{x}."
puts "It also allows \nbackslash notation."

puts
puts 'Single quoted string does not allow substitution for example #{x}.'
puts 'It allows limited backslash notation like \\ but not \n.'

The output of the above code will be:

Ruby String Data Type.
Double quoted string allow substitution like this.
It also allows 
backslash notation.

Single quoted string does not allow substitution for example #{x}.
It allows limited backslash notation like \ but not \n.

Ruby Boolean

Boolean data type represents only one bit of information either true or false.

Example:

The example below shows the usage of Boolean data type.

if true then
  puts "It is true."
else
  puts "It is false."
end

if 0 then
  puts "0 is true."
else
  puts "0 is false."
end

if nil then
  puts "nil is true."
else
  puts "nil is false."
end

The output of the above code will be:

It is true.
0 is true.
nil is false.

Ruby Hashes

A hash assign its values to its key. A value is assigned to a key by using => sign. A key/value pair is separated using comma , and all the pairs are enclosed within curly braces. A trailing comma is ignored.

Example:

The example below shows the usage of Hashes data type.

weekdays = {1 => "MON",
            2 => "TUE",
            3 => "WED",
            4 => "THU",
            5 => "FRI"}

#iterating over weekdays
weekdays.each do |key, value|
 puts "#{key} = #{value}"
end

The output of the above code will be:

1 = MON
2 = TUE
3 = WED
4 = THU
5 = FRI

Ruby Arrays

An array stores data or list of data. It can contain all types of data. Data in an array are separated using comma , and all elements are enclosed within square bracket. The position of elements in an array starts with 0. A trailing comma is ignored.

Example:

The example below shows the usage of Arrays data type.

MyArray = ["RED", "BLUE", "GREEN"]

#iterating over MyArray
MyArray.each do |color|
 puts "color = #{color}"
end

The output of the above code will be:

color = RED
color = BLUE
color = GREEN

Ruby Symbols

Symbols are light-weight strings. A symbol is preceded by a colon (:). They are used instead of strings because they require significantly less memory and therefore have better performance.

Example:

The example below shows the usage of Symbols data type.

MyArray = [:Hello, :World]

#iterating over MyArray
MyArray.each do |i|
 puts "i = #{i}"
end

The output of the above code will be:

i = Hello
i = World