Welcome! Today's subject is encapsulation, a cornerstone of object-oriented programming (OOP). Encapsulation bundles data and the operations that we perform on them into one unit, namely, an object. It protects data from unwanted alterations, ensuring the creation of robust and maintainable software.
Prepare yourself for an exciting journey as we delve into how encapsulation works in Ruby and explore the vital role it plays in data privacy.
Starting with the basics, encapsulation involves combining data and the methods that modify this data into a single unit known as a class
. It protects the data within an object from external interference.
In classes, you will often work with instance variables. These variables are declared with a @
in front and are central to encapsulation in Ruby. They hold data specific to an object and persist across different methods within the same object. These variables are private by default, meaning they can’t be accessed directly from outside the object, which helps maintain data privacy and integrity.
To illustrate, consider a Ruby class
representing a bank account. Without encapsulation, the account balance would be accessible to outside functions and subject to unsolicited alterations from within. However, with encapsulation, we can provide a standardized and streamlined approach to alter the balance, like depositing or withdrawing.
Encapsulation restricts direct access to an object's data and prevents unwanted data alteration. This principle is comparable to window blinds, allowing you to look out while preventing others from peeping in.
In Ruby, encapsulation pertains to marking methods as private, which is integral to data privacy. Private methods can only be accessed from within the class where they are defined.
To illustrate, let's consider a Ruby class
named Person
, which includes a private method name
.
In this example, name
is private, and get_name
enables us to access name
. We specify that direct access to the name
method is not allowed.
In encapsulation, getters and setters are used to control access to an object's attributes. Initially, you can manually define these methods within a class to handle the reading and writing of instance variables. For example:
However, manually creating these methods can be a bit verbose. Thankfully, Ruby offers a more concise approach through attr_reader
and attr_writer
. These methods streamline the code by automatically creating the getter and setter methods for you:
For even greater simplicity, Ruby provides attr_accessor
, which combines both functionalities into a single line, creating both the getter and setter methods seamlessly:
By utilizing these Ruby features, managing class attributes becomes more efficient and aligns well with encapsulation principles, maintaining clean and readable code.
Let's apply the principle of encapsulation to our BankAccount
class, which includes private attributes like account number and balance, along with public methods for withdrawals, deposits, and balance checks.
In the above code, the BankAccount
class encapsulates account details, and the attribute accessors manage the balance in a controlled manner.
Admirable! Now it's your turn to apply what you've learned by practicing encapsulation in Ruby. Remember, practice enhances your comprehension. Enjoy coding!
