×

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

Encapsulation in Ruby

By IncludeHelp Last updated : November 16, 2024

In this article, we will study about Encapsulation. We know that Ruby is a pure object-oriented language and in every object-oriented language, encapsulation is considered as one of the important properties. So, let us understand encapsulation with the help of program codes and syntaxes.

Encapsulation

Wrapping up the data into a single unit is known as Encapsulation. It can also be called as the procedure to bind code and the data which is affected by the code together. By applying encapsulation you can protect your data from being manipulated by another source. In simpler words, it can be considered as the mechanism which allows data only to be manipulated by the member functions of the class in which they are declared.

How to Achieve Encapsulation?

You can achieve encapsulation by declaring all the variables of class as private( they are by default private) and all the member functions as public(they are by default public). Now, these variables will only be accessed by these public methods of the class.

Advantages of Encapsulation

  • Easy to test code: Unit testing is the testing that is known to every programmer. After achieving encapsulation, unit testing becomes easy to be done.
  • Decreases Redundancy: Encapsulation helps with making the code reusable. You can update the code as per the requirement of time.
  • Data Hiding: Encapsulation facilitates data hiding which means that user will not be able to get an idea about the internal implementation of the class. The user will be unaware of where the data is being stored in the class.

Example of Ruby Encapsulation

Let us understand encapsulation with the help of an example:

=begin Ruby program to demonstrate encapsulation =end class Bank def initialize(id, name, account_no, type) @cust_id = id @cust_name = name @ac_no = account_no @cust_type = type end def display_details puts "Customer ID : #{@cust_id}" puts "Customer Name : #{@cust_name}" puts "Account No : #{@ac_no}" puts "Account Type : #{@cust_type}" puts "-" * 30 end end # Create customer instances customer1 = Bank.new("Cust101", "Rashmeet", "AC789", "Savings") customer2 = Bank.new("Cust102", "Parmeet", "AC1789", "Savings") customer3 = Bank.new("Cust103", "Jagmeet", "AC2789", "Savings") # Display customer details customer1.display_details customer2.display_details customer3.display_details

Output

Customer ID   : Cust101
Customer Name : Rashmeet
Account No    : AC789
Account Type  : Savings
------------------------------
Customer ID   : Cust102
Customer Name : Parmeet
Account No    : AC1789
Account Type  : Savings
------------------------------
Customer ID   : Cust103
Customer Name : Jagmeet
Account No    : AC2789
Account Type  : Savings
------------------------------

Explanation

In the above code you can observe that the member functions and data are only accessible through the object or instance of the class. Here the instances of class "Bank" are customer1, customer2 and customer3.

Another Example of Ruby Encapsulation

Here’s another example of encapsulation in Ruby, where private attributes are accessed and modified through getter and setter methods:

=begin Ruby program to demonstrate encapsulation with getter and setter methods =end class Employee # Constructor to initialize employee details def initialize(id, name, salary) @emp_id = id @emp_name = name @emp_salary = salary end # Getter for employee ID def emp_id @emp_id end # Getter for employee name def emp_name @emp_name end # Getter for employee salary def emp_salary @emp_salary end # Setter for employee salary def emp_salary=(new_salary) if new_salary > 0 @emp_salary = new_salary else puts "Invalid salary amount!" end end # Display employee details def display_details puts "Employee ID : #{@emp_id}" puts "Employee Name : #{@emp_name}" puts "Employee Salary: #{@emp_salary}" puts "-" * 30 end end # Create an employee instance employee = Employee.new("E001", "Alice", 5000) # Display initial details employee.display_details # Update salary using the setter method employee.emp_salary = 6000 # Try setting an invalid salary employee.emp_salary = -2000 # Display updated details employee.display_details

Output

Employee ID    : E001
Employee Name  : Alice
Employee Salary: 5000
------------------------------
Invalid salary amount!
Employee ID    : E001
Employee Name  : Alice
Employee Salary: 6000
------------------------------
Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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