Data Abstraction in Ruby

The idea of representing significant details and hiding details of functionality is called data abstraction. The interface and the implementation are isolated by this programming technique. Data abstraction is one of the object oriented programming features as well. Abstraction is trying to minimize information so that the developer can concentrate on a few ideas at a time. Abstraction is the foundation for software development.

Consider a real-life example of making a phone call. The only thing the person knows is that typing the numbers and hitting the dial button will make a phone call, they don’t know about the inner system of the phone or the dial button on the phone. That’s what we call abstraction.
Another real-life example of abstraction is as users of television sets, we can switch it on or off, change the channel and set the volume without knowing the details about how its functionality has been implemented.

Data Abstraction in modules: In Ruby, Modules are defined as a set of methods, classes, and constants together.For example, consider the sqrt() method present in Math module. Whenever we need to calculate the square root of a non negative number, We simply call the sqrt() method present in the Math module and send the number as a parameter without understanding the actual algorithm that actually calculates the square root of the numbers.

Data Abstraction in Classes: we can use classes to perform data abstraction in ruby. The class allows us to group information and methods using access specifiers (private, protected, public). The Class will determine which information should be visible and which is not.

Data Abstraction using Access Control: There are three levels of access control in Ruby (private, protected, public). These are the most important implementation of data abstraction in ruby. For Example-



  • Members who have been declared public in a class can be accessed from anywhere in the program.
  • Members declared to be private in a class can only be accessed from within the class. They are
    not allowed to access any part of the code outside the class.
filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to demonstrate Data Abstraction 
  
class Geeks 
  
    # defining publicMethod 
      
    public
  
    def publicMethod 
        puts "In Public!"
        # calling privateMethod inside publicMethod 
        privateMethod
    end
  
    # defining privateMethod 
      
    private 
  
    def privateMethod 
        puts "In Private!"
    end
end
  
  
# creating an object of class Geeks 
obj = Geeks.new
  
# calling the public method of class Geeks 
obj.publicMethod

chevron_right


Output:

In Public!
In Private!

In the above program, we are not allowed to access the privateMethod() of Geeks class directly, however, we can call the publicMethod() in the class in order to access the privateMethod().

Advantages of Data Abstraction:

  • Helps increase the security of a system because only crucial details are made available to the user.
  • It increases re-usability and prevents redundancy of code.
  • Could alter the internal class implementation independently without affecting the user.
My Personal Notes arrow_drop_up

If you only do what you can do, you will never be more than what you are ~Shifu

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.