How to implement Data Abstraction in Ruby?


Abstraction is an object-oriented programming concept, where the essential attributes of something are shown and all the unnecessary ones are hidden. With this approach, one can hide the implementation details and provide only the important interface.

A very good example of abstraction is your car. It's a great example of abstraction. When you start the car by turning the key or pressing the start button, you don't necessarily need to know how the engine works or how it gets started, also what components are present in that car or what else is there. These details that are irrelevant to the driver are simply hidden from the user as there's no need to add complexity, hence the data abstraction works just like that in Ruby.

Now, we have discussed what abstraction is in general programming terms. Now, let's talk about abstraction in Ruby.

Abstraction in Ruby happens at three levels (or ways). We will be discussing the three ways below.

Data Abstraction in Modules

We know that when it comes to modules in Ruby, there are different methods defined inside the different modules that we can use. consider the case of the sqrt() method, which we can invoke and pass the number inside it. We don't have to worry how that sqrt() method is actually calculating the square root.

Data Abstraction in Classes

One can use different access specifiers to control information and perform data abstraction in Ruby.

Data Abstraction Using Actress Control

In Ruby, we can use access specifiers like private, public and protected to achieve data abstraction.

When we declare a member as public in a class, then that member can be accessed from anywhere in the program. If we declare a member as private, then that member can only be accessed from within the class.

Let's consider a simple example where we will declare a class and define them with the help of two methods, where the first method is public and the second one will be private in nature.

Example 1

Consider the code shown below

class Coders

   # defining the public Method
   public
   def publicMethod
      puts "In Public Method!"
   end

   # defining the private Method
   private
   def privateMethod
      puts "In Private Method!"
   end

end

# creating an object of class Coders
object1 = Coders.new

# calling the public method of class Coders
object1.publicMethod

Output

When we execute this code, it will produce the following output −

In Public Method!

Example 2

In this example, we will try to call the method that was declared private from another class and see what happens.

class Coders
   # defining the public Method
   public
   def publicMethod
      puts "In Public Method!"
   end

   # defining privateMethod
   private
   def privateMethod
      puts "In Private Method!"
   end
end

class Sample
   private
   def privateMethod
      puts "Trying..."
   end
end

# creating an object of class Coders
obj1 = Coders.new
obj2 = Sample.new
obj1.publicMethod
obj2.privateMethod

Output

If we write the following code on any Ruby IDE, then we will get the following output in the terminal.

In Public Method!
Traceback (most recent call last):
main.rb:34:in `<main>': private method `privateMethod' called for #<Sample:0x00005573b8da04e0> (NoMethodError)

Updated on: 12-Apr-2022

786 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements