Ruby

Ruby – Arrays & Hashes

Ruby has a built-in class of Arrays and Hashes. These two classes have large interfaces and will take time to master. Ruby has also a block syntax that lets you encapsulate chunks of code. These blocks can become powerful iterator constructs. Arrays The class Arrays holds a collection of object references. Each object reference occupies

Ruby

Ruby – Access Control

It is important to consider how much of your class you’ll be exposing to the outside world. Ruby give you three levels of protection. Public methods – can be called by anyone. Methods are public by default. Initialize method is always private. Protected methods – can be invoked by objects of the defining class and

Ruby

Ruby – Virtual Attributes

Virtual attributes can be confusing but they help a lot in making things flexible especially when you trying to simplify things while working with Rails. Essentially, we create an attribute that is based on existing attributes. We can also define setter methods for these virtual attributes so that anyone can assign to them, mapping the

Rails

Ruby – Variables

Variables are used to keep track of objects. Each variable holds a reference to an object. In Ruby, strings are mutable, unlike Java. Assigning person1 to person2 doesn’t create any new objects. It simply copies person1’s object reference to person2, so that both variables refer to the same object. Duplicating String Objects You can use

Ruby

Ruby – Accessor Methods

Objects and Attributes We introduce states into an object through the initialize method. Other than creating an object, it is important to also be able to access and manipulate the state of an object. One way to do that is to write accessor methods Getter Function By defining the isbn and price (accessor) method, it

Ruby

Ruby – Initialize Method

How do we create a class? Note: Class will start with an uppercase letter and method will start with a lowercase letter To instantiate an object from a class (i.e. create new instance of a class), we use the new method. The problem with the above code is that both instances are identical. There is

OOP