Ruby/Akcesory: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Kj (dyskusja | edycje)
m nav
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Linia 1:
== Akcesory ==
What is an accessor?
 
=== Czym jest akcesor? ===
We briefly discussed instance variables in an earlier chapter, but haven't done much with them yet. An object's instance variables are its attributes, the things that distinguish it from other objects of the same class. It is important to be able to write and read these attributes; doing so requires methods called attribute accessors. We'll see in a moment that we don't always have to write accessor methods explicitly, but let's go through all the motions for now. The two kinds of accessors are writers and readers.
 
Krótko omówiliśmy zmienne instancji we [[Ruby/Zmienne instancji|wcześniejszym rozdziale]], ale za wiele jeszcze z nimi nie robiliśmy. Zmienne instancji obiektu są jego atrybutami, to te rzeczy, które odróżniają obiekt od innych obiektów tej samej klasy. To ważne, by móc zapisywać i odczytywać te atrybuty; a robią to metody nazywane ''akcesorami atrybutów''. Zobaczymy za moment, że nie musimy pisać akcesorów bezpośrednio, ale teraz przejdźmy przez wszystkie etapy. Wyróżniamy dwa rodzaje akcesorów: ''zapisujące'' i ''odczytujące''.
 
<pre>
ruby> class Fruit
| def set_kind(k) # a writer
Linia 20 ⟶ 23:
ruby> f1 # inspect the object
#<Fruit:0xfd7e7c8c @kind="peach">
</pre>
 
Wystarczająco proste; możemy przechowywać i odczytywać informację o tym, na jaki owoc patrzmy. Ale nasze nazwy metod są nieco rozwlekłe. W następującym przykładzie są nieco bardziej zwięzłe oraz bardziej konwencjonalne:
Simple enough; we can store and retrieve information about what kind of fruit we're looking at. But our method names are a little wordy. The following is more concise, and more conventional:
 
<pre>
ruby> class Fruit
| def kind=(k)
Linia 38 ⟶ 43:
ruby> f2.kind
"banana"
</pre>
The inspect method
 
=== Metoda <tt>inspect</tt> ===
A short digression is in order. You've noticed by now that when we try to look at an object directly, we are shown something cryptic like #<anObject:0x83678>. This is just a default behavior, and we are free to change it. All we need to do is add a method named inspect. It should return a string that describes the object in some sensible way, including the states of some or all of its instance variables.
 
Jest tu potrzebna krótka dygresja. Z pewnością zauważyłeś, że jeżeli próbujemy spojrzeć na obiekt bezpośrednio, pokazuje się nam coś zagadkowego w rodzaju <tt>#<anObject:0x83678></tt>. To jest po prostu domyślne zachowanie i oczywiście możemy je zmienić. Wszystko, co musimy zrobić to dodać metodę o nazwie <tt>inspect</tt>. Powinna ona zwracać łańcuch, który opisuje obiekt w jakiś sensowny sposób, włączając stany części lub wszystkich jego zmiennych instancji.
 
<pre>
ruby> class Fruit
| def inspect
Linia 50 ⟶ 58:
ruby> f2
"a fruit of the banana variety"
</pre>
 
Podobną metodą jest metoda <tt>to_s</tt> (ang. ''convert to string'' - zamień na łańcuch), która jest używane gdy drukujemy obiekt. Ogólnie rzecz biorąc, możesz traktować <tt>inspect</tt> jako narzędzie, gdy piszesz i debugujesz programy, natomiast <tt>to_s</tt> jako sposób na oczyszczenie wyjścia programu. ''eval.rb'' używa <tt>inspect</tt> ilekroć wyświetla wyniki. Możesz użyć metody <tt>p</tt> by łatwo debugować wyjście z programów.
A related method is to_s (convert to string), which is used when printing an object. In general, you can think of inspect as a tool for when you are writing and debugging programs, and to_s as a way of refining program output. eval.rb uses inspect whenever it displays results. You can use the p method to easily get debugging output from programs.
 
<pre>
# These two lines are equivalent:
# Te dwie linie są równoważne:
p anObject
puts anObject.inspect
</pre>
 
Making accessors the easy way