📜 ⬆️ ⬇️

Let's beat Ruby together! Drop four

We collect drops further ( 1 , 2 , 3 ). This time we will learn about the implementation of the PLO in Ruby.

Classes, objects, methods


Instead of delving further into the Ruby syntax, set aside loops, types, modules, etc., we will return to them later. Now we will see how to create classes, objects and methods.

As in other object languages, objects are defined by a class. For example, this class defines dogs (o_0):

 class Dog def set_name( aName ) @myname = aName end end 

Well, what do you dogs - not class? ;) We look code. The class definition begins with the class keyword and the name of the class itself, which must begin with a capital letter . The class defines the method set_name ( def...end ), which will call each object-dog. It takes the input argument aName and assigns its value to the @myname variable.
')
Variables beginning with @ are class instance variables . It is important that the value of such a variable is available only within the instance to which it belongs. Create two objects (two specific dogs) using the new method:

 moya = Dog.new tvoya = Dog.new 

I remind and remember: we call the class only with a capital letter, the object - only with a lowercase. Use the set_name method to name the dogs:

 moya.set_name( 'Lassie' ) tvoya.set_name( 'Rex' ) 

How now to display the nicknames of dogs? We cannot “pull out” the @name variable from the object, since the internal data of the object is known only to it (see above). This is the basic principle of OOP: object data is private; this is called data hiding and is part of the encapsulation principle.

To solve this problem, simply add a new method to the class that will display the variable:

 def get_name return @myname end 

The word return optional, Ruby will return the last value received, but writing it is a good habit.

Let's make the dog bark (for this we will write another method) and collect everything in a heap:

 class Dog def set_name( aName ) @myname = aName end def get_name return @myname end def gav return 'rrrr!' end end dog1 = Dog.new dog1.set_name( 'Fido' ) puts(dog1.get_name) puts(dog1.gav) 

Imagine that we have two classes (for example, cats and dogs), each is determined by the object (specific cat and dog) and the method with the same name ("speak", but the cat meows, and the dog barks). It turns out that the objects react to the same method differently. The ability to have several classes containing methods of the same name is called polymorphism . They found out ... and forgot this terrible word.

Attributes


For our task there is another approach - we will create the necessary methods using class attributes . Complicate the task and create a zoo;)

 class Cat attr_accessor :name, :age, :gender, :color end class Dog attr_accessor :name, :age, :gender, :color end class Snake attr_accessor :name, :age, :gender, :color, :length end 

Consider the second line. It provides three attributes for the Cat class. Each cat has its own nickname, age, gender and color - and the code creates these attributes. _accessor means "make these attributes available for creation and modification."

Create an object and apply the attributes:

cat_object = Cat.new
cat_object.name = "Pussy"
........................
puts cat_object.name


Inheritance


Looking at the last code with the declaration of the three classes, it is easy to notice that they are extremely similar. Only the snake has an additional length. Recall the principles of programming in Ruby from the first drop. One of them: DRY (Don't repeat yourself), which means that we have to write the necessary code only once, and we have solid repetitions. We just need to pay attention to one of the best features of OOP: inheritance .

Inheritance allows different classes to relate to each other and group by similarities in the structure. In our example, cats, dogs, snakes are all pets. So why don't we create a Pet class that will be “parental” for the rest, which, in turn, will inherit properties common to all pets? We write:

 class Pet attr_accessor :name, :age, :gender, :color end class Cat < Pet end class Dog < Pet end class Snake < Pet attr_accessor :length end 

I think, the principle and rules of declaring inherited classes are clear, now try to create objects, set parameters and display the result. Note that simpler classes are higher than the classes that are complex in the hierarchy .

Epilogue


In the last drop, we figured out what the OOP is, in this one we learned how to apply knowledge in Ruby, learned a few muddled words, got acquainted with instance variables and class attributes. Another drop in our glass;)

Thanks to Hugh Colinborn and Peter Cooper for great examples for our drops. Comments and comments as always are accepted! See you!

Source: https://habr.com/ru/post/48756/


All Articles