| Start |
|---|
| Constructor A class can have only one constructor, that is a method called |
| Creating a class You can define a new class using the Once defined, you can create a new instance using the |
| Access Levels Ruby has three access levels. They are Methods that follow the Public MethodsA public method should describe the behavior of the object being created. These methods can be called from outside the scope of the created object. These methods are public ruby methods, they describe the behavior for initializing a new cat and the behavior of the speak method.
Private MethodsPrivate methods are not accessible from outside of the object. They are used internally by the object. Using the cat example again: As you can see in the example above, the newly created Cat object has access to the When we try and call the Protected MethodsProtected methods are very similar to private methods. They cannot be accessed outside the instance of object in the same way private methods can't be. However, using the You can see we've added an age parameter to the cat class and created three new cat objects with the name and age. We are going to call the Look at that, we were able to retrieve cat1's age using the |
| Accessing instance variables with getters and setters We have three methods:
Note that the parameters are symbols. this works by creating a method. Is basically the same as: |
| Class Methods types Classes have 3 types of methods: instance, singleton and class methods. Instance MethodsThese are methods that can be called from an Class MethodThese are static methods, i.e, they can be invoked on the class, and not on an instantiation of that class. It is equivalent to use Invoke the method by writing Singleton MethodsThese are only available to specific instances of the class, but not to all. Both the Another way of doing this is by the |
| Dynamic class creation Classes can be created dynamically through the use of In the above example, a new class is created and assigned to the constant The The |
| New, allocate, and initialize In many languages, new instances of a class are created using a special
Note also that the return value of The 'normal' |
| Class and instance variables There are several special variable types that a class can use for more easily sharing data. Instance variables, preceded by Class variable, preceded by Global Variables, preceded by Syntax
RemarksClass names in Ruby are Constants, so the first letter should be a capital. |
Read Article
https://develop.horje.com/learn/1434/reference