Procedural data abstraction in ruby

I am new to Ruby. I am learning the principle of abstraction in ruby. As I understand it, procedural abstraction hides implementation details from the user or simply concentrates on the basic principles and ignores the details.

My concern is how to implement it.

1) Is this a simple function calling it

# function to sort array # @params array[Array] to be sort def my_sort(array) return array if array.size <= 1 swapped = false while !swapped swapped = false 0.upto(array.size-2) do |i| if array[i] > array[i+1] array[i], array[i+1] = array[i+1], array[i] swapped = true end end end array end 

and calling it

 sorted_array = my_sort([12,34,123,43,90,1]) 

2) How data abstraction differs from encapsulation

As I understand it, Data Abstraction simply hides some member data from other classes.

+6
source share
3 answers

Data abstraction is fundamental to most object-oriented languages โ€‹โ€‹โ€” in which classes are designed to encapsulate data and provide methods for controlling how that data changes (if at all) or helper methods to get the value of that data.

The Ruby Array class is an example of data abstraction. It provides a mechanism for managing an array of objects and provides operations that can be performed in this array, without having to worry about how it is organized inside it.

 arr = [1,3,4,5,2,10] p arr.class # Prints Array p arr.sort # Prints [1,2,3,4,5,10] 

Procedural abstraction is to hide the details of the implementation of the procedure from the user. In the above example, you donโ€™t need to know which sort method sort used internally, you just use it, assuming that the good people in the Ruby Core team have chosen the best option for you.

At the same time, Ruby may not know how to always compare the two elements present in Array . For example, the code below will not work, since Ruby does not know how to compare strings and numbers.

 [1,3,4,5,"a","c","b", 2,10].sort #=> `sort': comparison of Fixnum with String failed (ArgumentError) 

This allows us to connect to the implementation and help in comparison, although the basic sorting algorithm remains the same (as it abstracts from the user)

 [1,3,4,5,"a","c","b", 2,10].sort { |i,j| if i.class == String and j.class == String i <=> j elsif i.class == Fixnum and j.class == Fixnum i <=> j else 0 end } #=> [1, 3, 4, 5, 2, 10, "a", "b", "c"] 

When writing code for your own problems, procedural abstraction can be used to ensure that a procedure often violates its problem in subtasks and solves each problem using a separate procedure. This allows some aspects to be expanded later (as in the above case, the comparison can be extended - thanks to Ruby blocks it was much easier). The template method template is a good technique to achieve this.

+4
source

You are returning an array from a method. Data structures are implementation details. If you change the data structure used in the method, you break the client code. Thus, your example does not hide implementation details. It does not encapsulate design decisions, so the client is isolated from the internal implementation details.

+3
source

The definition of "Abstraction": the quality of communication with ideas, not events.

Referring to this answer is the difference between abstraction and encapsulation? , and I realized that in your code the my_sort method fully justifies Encapsulation , because it encapsulates the behavior associated with sorting any single dimension array. However, it lacks abstraction , since the my_sort method knows the type of data it will process.

It would be justified to abstraction if he did not know / did not care about the type of data that comes through params. In other words, it had to sort any object that comes up whether it is a Fixnum or String list or other sortable datatypes .

Encapsulation :

Usually we use access modifiers ( public , private , ..) to distinguish between the data / behavior that should be exposed to clients and that should be used internally . The public interface (affected by customers) is not subject to change as much as possible. However, private is behavior that can change and should in no way affect the expected behavior of the code that clients rely on.
We also share confidential data / behavior as private / protected to prevent accidental modification / misuse. This forces the client not to rely on a piece of code that can change frequently.

  • Therefore, you always need to highlight the core logic in private .

Abstract

Example: In the case of the church, there is an abstraction between confessor and father / priest . The confessor should not have any idea of โ€‹โ€‹the name or any details of the priest and vice versa. Anyone can admit and yet hide his identity no matter what big mistakes / crimes he made.

+1
source

All Articles