Ruby/Moduły: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Szymon wro (dyskusja | edycje)
mNie podano opisu zmian
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Linia 1:
== Moduły ==
Modules in ruby are similar to classes, except:
 
Moduły w Rubim są podobne do klas, ale:
* A module can have no instances.
* Moduł nie może mieć instancji.
* A module can have no subclasses.
* Moduł nie może mieć podklas.
* A module is defined by module ... end.
* Moduł jest definiowany przez słowa kluczowe <tt>module</tt> i <tt>end</tt>.
 
ActuallyWłaściwie... theklasa <tt>Module</tt> classmodułu ofjest modulenadklasą is the superclass of theklasy <tt>Class</tt> class of classklasy. Got thatRozumiesz? NoNie? Let's moveIdźmy ondalej.
 
Istnieją dwa typowe zastosowania modułów. Jedno to zebranie powiązanych metod i stałych w jednym centralnym miejscu. Moduł <tt>Math</tt> z standardowej biblioteki Rubiego odgrywa taką rolę:
There are two typical uses of modules. One is to collect related methods and constants in a central location. The Math module in ruby's standard library plays such a role:
 
<pre>
ruby> Math.sqrt(2)
1.41421
ruby> Math::PI
3.14159
</pre>
 
Operator <tt>::</tt> mówi interpreterowi Rubiego, który moduł powinien on sprawdzić by pobrać wartość zmiennej (możliwe, że jakiś moduł oprócz <tt>Math</tt> może oznaczać coś innego prze <tt>PI</tt>). Jeżeli chcemy odnieść się do metod lub stałych modułu bezpośrednio, bez używania <tt>::</tt>, możemy "zawrzeć" ten moduł używając słowa kluczowego <tt>include</tt>:
The :: operator tells the ruby interpreter which module it should consult for the value of a constant (conceivably, some module besides Math might mean something else by PI). If we want to refer to the methods or constants of a module directly without using ::, we can include that module:
 
<pre>
ruby> include Math
Object
Linia 22 ⟶ 26:
ruby> PI
3.14159
</pre>
 
Inne użycie modułu nazywane jest ''mixin'' (zmieszanie?). Niektóre języki obiektowe, włączając C++, pozwalają na wielokrotne dziedziczenie, to znaczy, dziedziczenie po więcej niż jednej nadklasie. Przykładem wielokrotnego dziedziczenia ze codziennej rzeczywistości jest budzik. Możesz zarówno zaliczyć budziki do klasy zegarków jak i do klasy przedmiotów z brzęczykami.
Another use of modules is called mixin. Some OO programming langages, including C++, allow multiple inheritance, that is, inheritance from more than one superclass. A real-world example of multiple inheritance is an alarm clock; you can think of alarm clocks as belonging to the class of clocks and also the class of things with buzzers.
 
Ruby celowo nie implementuje prawdziwego wielokrotnego dziedziczenia, ale technika ''mixin'' jest dobrą alternatywą. Pamiętaj, że moduły nie mogą być instancjonowane, ani nie mogą mieć podklas. Ale jeśli włączymy (<tt>include</tt>) moduł w definicję klasy, jego metody będą efektywnie dodane czy też "wmieszane" w klasę.
Ruby purposely does not implement true multiple inheritance, but the mixin technique is a good alternative. Remember that modules cannot be instantiated or subclassed; but if we include a module in a class definition, its methods are effectively appended, or "mixed in", to the class.
 
''Mixin'' można rozważać jako sposób pytania o wszelkie partykularne własności, które chcemy mieć. Na przykład, jeżeli klasa ma działającą metodę <tt>each</tt>, zmieszanie jej ze standardowym modułem <tt>Enumerable</tt> da nam metody <tt>sort</tt> oraz <tt>find</tt> za darmo.
Mixin can be thought of as a way of asking for whatever particular properties we want to have. For example, if a class has a working each method, mixing in the standard library's Enumerable module gives us sort and find methods for free.
 
Takie użycie modułów dostarcza podstawowej funkcjonalności wielokrotnego dziedziczenia, ale pozwala nam reprezentować relacje pomiędzy klasami za pomocą prostych struktur drzewiastych, i w ten sposób upraszcza to znacząco implementację języka (podobny punkt widzenia został przyjęty przez projektantów Javy).
This use of modules gives us the basic functionality of multiple inheritance but allows us to represent class relationships with a simple tree structure, and so simplifies the language implementation considerably (a similar choice was made by the designers of Java).
<noinclude>
{{ProstaNawigacja|spis=Ruby|poprzart=Ruby/Metody singletonowe|poprz=Metody singletonowe|nastart=Ruby/Procedury jako obiekty|nast=Procedury jako obiekty}}