There are few things every serious Ruby programmer must know about. These things include concepts such as access modifiers, to_s method, modules, mixins, structs, procs and lambdas. Learn about all these concepts and more in this part of Getting started with Ruby. Expand your knowledge and become an advanced Ruby programmer!

Getting Started With Ruby the Easy Way Part 1 (Comments, Variables, Strings).

Getting Started With Ruby the Easy Way Part 2 (Data Types Pt1).

Getting Started With Ruby the Easy Way Part 3 (Data Types Pt2, Control Flow Pt1).

Getting Started With Ruby the Easy Way Part 4 (Control Flow Pt2).

Getting Started With Ruby the Easy Way Part 5 (Control Flow Pt3).

Getting Started With Ruby the Easy Way Part 6 (Methods).

Getting Started With Ruby the Easy Way Part 7 (Recursion, Scope, OOP Pt1).

Getting Started With Ruby the Easy Way Part 8 (OOP Pt2).

Getting Started With Ruby the Easy Way Part 9 (OOP Pt3).

Ruby and OOP (Object-Oriented Programming) pt4

Access modifiers

So far, all the methods you have seen through this mini series were defined as publicly available. This means that you could call any of these methods from outside the class and they would work. However, there are situations where this behavior may not be what you want. In those situations you may want some methods to be visible only to the class.

One example of such a situation can be application for a Bank. Imagine you have a class containing methods to calculate values such as account balance, internal transactions, interests and so on. Making any of these methods available outside the class is not something you would want. That would be a significant security flaw.

Fortunately, Ruby provides a way to specify how visible your methods should be. In Ruby, this concept is called “access modifiers” and there are three access modifiers you can use-“public”, “private” and “protected”. These access modifiers can be applied only to methods. Instance variable are always private.

Public

Any time you define a new class method, Ruby interprets it as public by default. The only exception is initialize. This means that any class method, other than initialize, is accessible and visible from both, the inside as well as the outside of the class.

#ruby #programming #design development

Getting Started With Ruby the Easy Way Pt10
2.00 GEEK