Home » Ruby programming

Objects in Ruby

Ruby objects: Here, we are going to learn about the objects in Ruby, types of ruby objects, object creation with syntaxes and examples.
Submitted by Hrithik Chandra Prasad, on July 21, 2019

Ruby objects

We are well aware of the fact that Ruby is an object-oriented scripted programming language. Thus, everything we do in Ruby is directly related to objects. Ruby program can also be considered as a stream of tokens. The tokens are nothing but various Ruby keywords, operators, and a variety of literals. Objects stand as the main part of the Ruby program if the semantic view is contemplated.

Objects are created, manipulated and destroyed during the execution of a Ruby program.

We have got two types of objects in Ruby:

  1. Built-in objects
    Ruby has a rich set of libraries for increasing the efficiency of programming. Built-in objects come from those pre-defined libraries. They are available to the application programmers and they can use it as per the requirement of their code.
  2. Custom objects
    Custom objects are created by the application programmers according to the demand of their program before their usage in the code.

Object creation can also be called as Object instantiation. An object consisted of two things: the first one is called Data and later one known as Method or Function.

"puts" and "gets" are the two most commonly used methods in our Ruby program. Let us see, which object they belong to.

Observe the following codes:

Code 1

puts "What is your name?"
name = gets.chomp
puts "Hi #{name}, Have a great day!!"

Code 2

Kernel.puts "What is your name?"
name = Kernel.gets.chomp
Kernel.puts "Hi #{name}, Have a great day!!"

You will find that puts and gets can also be used with a word known as Kernel and that Kernel is the actual object which encapsulates both the methods just like we have System.println in Java and Console.writeln in C#. puts and gets method is a part of the Kernel module.

We can also calculate the size of a string in the following manner.

Kernel.puts "What is your name?".size

Output

18

If you have done programming in other languages, then the above might be confusing for you as String is a primitive data type and does not have its methods. But we know that Ruby is different, Ruby employs String as a full object which has its methods. In the above code, we are using one of its methods size which eventually returns the size of the string.

Now, give String some rest and let us talk about Integers. Integers also come under primitive datatype in other languages but again in Ruby they are purely objects with certain methods. Let us see some of its methods.

puts 3.object_id
puts 4.even?
puts 9.odd?
puts 7.class

Output

7
true
true
Integer

Observe the above code carefully, you will find for methods of Integer object namely:

  1. .object_id
    Each object has got an id. This method object_id returns an id associated with the object.
  2. .even?/.odd?
    The method checks whether the integer is even or odd and returns the value as True or False.
  3. .class
    This method returns the class from which the object belongs.

Object creation

There are two ways in which an object can be created. Implicit object creation is done with the help of literal notation. Explicit object creation is the object by a "new" keyword. Custom objects are created with the help of a "new" keyword.

Consider the example given below for a better understanding of the concept,

str = String.new "Include Help"
puts str.size
puts str.upcase
puts str.downcase

Output

12
INCLUDE HELP
include help

The above example demonstrates the formal way of creating a String object. It is also showing some of the methods of the String object.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.