Ruby/Zmienne lokalne: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Linia 32:
</pre>
 
Obiekty procedurowe, które żyją w tym pewnym zakresie widoczności współdzielą tylko zmienne lokalne, które również należą do tego zakresu. Tutaj, zmienna lokalna <tt>bar</tt> jest współdzielona przez <tt>main</tt> oraz obiekty procedurowe <tt>p1</tt> i <tt>p2</tt>:
Procedure objects that live in the same scope share whatever local variables also belong to that scope. Here, the local variable bar is shared by main and the procedure objects p1 and p2:
 
<pre>
ruby> bar=nil
nil
Linia 46 ⟶ 47:
ruby> p2.call
5
</pre>
 
Zauważ, że "<tt>bar=nil</tt>" na początku nie może zostać pominięte. To przypisanie zapewnia, że zasięg zmiennej <tt>bar</tt> będzie obejmie <tt>p1</tt> i <tt>p2</tt>. Inaczej <tt>p1</tt> i <tt>p2</tt> postawiłyby by swoje własne zmienne lokalne <tt>bar</tt>, i rezultatem wywołania <tt>p2</tt> byłby błąd "undefined local variable or method" (niezdefinowana zmienna lokalna lub metoda). Moglibyśmy użyć <tt>bar=0</tt>, ale użycie <tt>nil</tt> jest pewną uprzejmością wobec tych, którzy będą czytać nasz kod później. Wskazuje to naprawdę jasno że tylko ustanawiamy zakres, ponieważ wartość przypisywana do zmiennej nie jest zawiera żadnego dodatkowego znaczenia.
Note that the "bar=nil" at the beginning cannot be omitted; it ensures that the scope of bar will encompass p1 and p2. Otherwise p1 and p2 would each end up with its own local variable bar, and calling p2 would have resulted in an "undefined local variable or method" error. We could have said bar=0 instead, but using nil is a courtesy to others who will read your code later. It indicates fairly clearly that you are only establishing scope, because the value being assigned is not intended to be meaningful.
 
Potężną zaleta obiektów procedurowych wypływa z ich zdolności do bycia przekazywanymi jako argumenty: współdzielone zmienne lokalne pozostają poprawde nawet wtedy, gdy są przekazane poza pierwotny zakres.
A powerful feature of procedure objects follows from their ability to be passed as arguments: shared local variables remain valid even when they are passed out of the original scope.
 
<pre>
ruby> def box
| contents = nil
Linia 66 ⟶ 69:
ruby> reader.call
2
</pre>
 
Ruby jest szczególnie sprytny jeśli chodzi o zakres. Ewidentnie widać w naszym przykładzie, że zmienna <tt>contents</tt> jest współdzielona pomiędzy <tt>reader</tt> i <tt>writer</tt>. Możemy również wytworzyć wiele par <tt>reader-writer</tt> używając <tt>box</tt> zdefiniowanego powyżej. Każda para współdzieli zmienną <tt>contents</tt>, a pary nie kolidują ze sobą nawzajem.
Ruby is particularly smart about scope. It is evident in our example that the contents variable is being shared between the reader and writer. But we can also manufacture multiple reader-writer pairs using box as defined above; each pair shares a contents variable, and the pairs do not interfere with each other.
 
<pre>
ruby> reader_1, writer_1 = box
[#<Proc:0x40172820>, #<Proc:0x4017280c>]
Linia 79 ⟶ 84:
ruby> reader_2.call # nothing is in this box yet
nil
</pre>
 
Ten rodzaj programowania mógłby być uważany za lekko perwersyjny zorientowany obiektowo szkielet. Metoda <tt>box</tt> odgrywa rolę klasy podczas gdy <tt>get</tt> i <tt>set</tt> służą jako metody (z wyjątkiem tego, że tak naprawdę nie są nazwami metod, co mogłoby różnić w każdą instancji <tt>box</tt>) natomiast <tt>contents</tt> jest samotną zmienną intancji. Oczywiście stosowanie właściwego szkieletu klas Rubiego prowadzi do znacznie bardziej czytelnego kodu.
This kind of programming could be considered a perverse little object-oriented framework. The box method acts something like a class, with get and set serving as methods (except those aren't really the method names, which could vary with each box instance) and contents being the lone instance variable. Of course, using ruby's legitimate class framework leads to much more readable code.
<noinclude>
{{ProstaNawigacja|spis=Ruby|poprzart=Ruby/Zmienne instancji|poprz=Zmienne instancji|nastart=Ruby/Stałe klasowe|nast=Stałe klasowe}}