Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Procedural and Data abstraction in ruby

I'm new to Ruby. I'm learning abstraction principle in ruby.As I understood Procedural abstraction is hiding the implementation details from the user or simply concentrating on the essentials and ignoring the details.

My concern is how to implement it

1) Is it a simple function calling just like this

# 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 like this

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

2) How does Data Abstraction differs from Encapsulation

As I understood Data Abstraction is just hiding some member data from other classes.

like image 448
Bibek Sharma Avatar asked Sep 11 '15 05:09

Bibek Sharma


People also ask

What is data abstraction and procedural abstraction?

Procedural abstraction: the separation of the logical properties of an action from the details of how the action is implemented. Data abstraction: the separation of the logical properties of data from the details of how the data are represented.

What is 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.

What is procedural abstraction in data structure?

Procedural abstraction is when we write code sections (called "procedures" or in Java, "static methods") which are generalised by having variable parameters. The idea is that we have code which can cope with a variety of different situations, depending on how its parameters are set when it is called.

What is encapsulation in Ruby?

Encapsulation is the ability to wrap the data into a single unit. In simple terms, it's a mechanism to wrap data and the code that manipulates the data. In Ruby, we can achieve encapsulation with the help of classes.


2 Answers

Data abstraction is fundamental to most object oriented language - wherein the classes are designed to encapsulate data and provide methods to control how that data is modified (if at all), or helper methods to derive meaning of that data.

Ruby's Array class is an example of Data Abstraction. It provides a mechanism to manage an array of Objects, and provides operations that can be performed on that array, without you having to care how internally it is organized.

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 about hiding implementation details of procedure from the user. In the above example, you don't really need to know what sorting algorithm sort method uses internally, you just use it assuming that nice folks in Ruby Core team picked a best one for you.

At the same time, Ruby may not know how to compare two items present in the Array always. For example, below code would not run as 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)

It allows us to hook into implementation and help with comparison, even though underlying sorting algorithm remains same (as it is abstracted 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 a procedure often breaks down its problem into sub-problems, and solves each sub-problems using separate procedure. This allows, certain aspects to be extended later (as in above case, comparison could be extended - thanks to Ruby blocks, it was much easier). Template method pattern is good technique to achieve this.

like image 116
Wand Maker Avatar answered Nov 15 '22 07:11

Wand Maker


You are returning an array from the method. Data structures are implementation details. If you change the data structure used in the method, you will break the client code. So your example does not hide the implementation details. It does not encapsulate the design decisions so that the client's are insulated from the internal implementation details.

like image 26
bparanj Avatar answered Nov 15 '22 06:11

bparanj