Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Data Types

  • Difficulty Level : Basic
  • Last Updated : 31 Jan, 2019

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

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

Numbers: Generally a number is defined as a series of digits, using a dot as a decimal mark. Optionally the user can use the underscore as a separator. There are different kinds of numbers like integers and float. Ruby can handle both Integers and floating point numbers. According to their size, there are two types of integers, one is Bignum and second is Fixnum.

  • Example:




    # Ruby program to illustrate the
    # Numbers Data Type
      
    # float type
    distance = 0.1
      
    # both integer and float type
    time = 9.87 / 3600
    speed = distance / time
    puts "The average speed of a sprinter is #{speed} km/h"

    Output:

    The average speed of a sprinter is 36.474164133738604 km/h

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

  • Example:






    # Ruby program to illustrate the
    # Boolean Data Type
      
    if true
      puts "It is True!"
    else
      puts "It is False!"
    end
      
    if nil
      puts "nil is True!"
    else
      puts "nil is False!"
    end
      
    if 0
      puts "0 is True!"
    else
      puts "0 is False!"
    end

    Output:

    It is True!
    nil is False!
    0 is True!

Strings: A string is a group of letters that represent a sentence or a word. Strings are defined by enclosing a text within a single (”) or double (“”) quotes. You can use both double quotes and single quotes to create strings. Strings are objects of class String. Double-quoted strings allow substitution and backslash notation but single-quoted strings doesn’t allow substitution and allow backslash notation only for \\ and \’.

  • Example:




    # Ruby program to illustrate the
    # Strings Data Type
      
    #!/usr/bin/ruby -w
    puts "String Data Type";
    puts 'escape using "\\"';
    puts 'That\'s right';

    Output:

    String Data Type
    escape using "\"
    That's right

Hashes: A hash assign its values to its key. Value to a key is assigned by => sign. A key pair is separated with a comma between them and all the pairs are enclosed within curly braces. A hash in Ruby is like an object literal in JavaScript or an associative array in PHP. They’re made similarly to arrays.e. A trailing comma is ignored.

  • Example:




    # Ruby program to illustrate the
    # Hashes Data Type
      
    #!/usr/bin/ruby
    hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
    hsh.each do |key, value|
     print key, " is ", value, "\n"
    end

    Output:

    red is 3840
    green is 240
    blue is 15

Arrays: An array stores 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 within square bracket.The position of elements in an array starts with 0. A trailing comma is ignored.

  • Example:




    # Ruby program to illustrate the
    # Arrays Data Type
      
    #!/usr/bin/ruby
    ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
    ary.each do |i|
     puts i
    end

    Output:

    fred
    10
    3.14
    This is a string
    last element

Symbols: Symbols are light-weight strings. A symbol is preceded by a colon (:). They are used instead of strings because they can take up much less memory. Symbols have better performance.

  • Example:




    # Ruby program to illustrate the
    # Symbols Data Type
      
    #!/usr/bin/ruby
    domains = {:sk => "GeeksforGeeks", :no => "GFG", :hu => "Geeks"}
      
    puts domains[:sk]
    puts domains[:no]
    puts domains[:hu]

    Output:

    GeeksforGeeks
    GFG
    Geeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :