×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Objects in Ruby

By IncludeHelp Last updated : December 01, 2024

Introduction to 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.

Ruby Objects

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

Types of Objects

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 application programmers, and they can use them 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

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.

Common Ruby Methods: puts and gets

"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:

Example 1

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

Example 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.

Ways to Create Objects

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.

Example

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.