Ruby/Struktury sterujące: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Szymon wro (dyskusja | edycje)
Nie podano opisu zmian
Szymon wro (dyskusja | edycje)
Linia 59:
</pre>
 
Czasami chcesz zanegować sprawdzany warunek. ''unless'' jest zanegowanym ''if'' a ''until'' zanegowanym ''while''. Poeksperymentuj jak chcesz z tymi dwoma instrukcjami.
Sometimes you want to negate a test condition. An unless is a negated if, and an until is a negated while. We'll leave it up to you to experiment with these.
 
Są cztery sposoby do przerwania postępu pętli z jej wnętrza. Pierwszy, ''break'' oznacza, tak jak w C, zupełną ucieczkę z pętli. Drugi, ''next'' przeskakuje na początek kolejnej iteracji pętli (podobnie jak znane z C ''continue''). Trzeci, to specyficzne dla Rubiego ''redo'', które oznacza ponowne wykonanie bieżącej iteracji. Następujący kod w języku C ilustruje znaczenia instrukcji ''break'', ''next'', i ''redo'':
There are four ways to interrupt the progress of a loop from inside. First, break means, as in C, to escape from the loop entirely. Second, next skips to the beginning of the next iteration of the loop (corresponding to C's continue). Third, ruby has redo, which restarts the current iteration. The following is C code illustrating the meanings of break, next, and redo:
 
<pre>
while (condition) {
label_redo:
goto label_next; /* ruby'sw Rubim: "next" */
goto label_break; /* ruby'sw Rubim: "break" */
goto label_redo; /* ruby'sw Rubim: "redo" */
...
...
Linia 77:
</pre>
 
Czwarty sposób by wyjść z pętli będąc w jej wnętrzu to ''return''. Obliczenie instrukcji ''return'' powoduje wyjście nie tylko z pętli ale również z metody która tę pętle zawiera. Jeżeli podany został argument, będzie on zwrócony jako rezultat wywołania metody. W przeciwnym wypadku zwracane jest ''nil''.
The fourth way to get out of a loop from the inside is return. An evaluation of return causes escape not only from a loop but from the method that contains the loop. If an argument is given, it will be returned from the method call, otherwise nil is returned.
for
 
=== for ===
C programmers will be wondering by now how to make a "for" loop. Ruby's for can serve the same purpose, but adds some flexibility. The loop below runs once for each element in a collection (array, hash, numeric sequence, etc.), but doesn't make the programmer think about indices:
 
Programiści C będą się zastanawiać jak zrobić pętlę "''for''". ''for'' Rubiego może służyć w ten sam sposób, choć jest nieco bardziej elastyczne. Pętla poniżej przebiega raz każdy element w kolekcji (tablicy, tablicy asocjacyjnej, sekwencji numerycznej, itd.), ale nie zmusza programisty do myślenia o indeksach:
 
<pre>
for elt in collection
# ... here,tutaj elt refersodnosi tosię ando elementelementu ofw the collectionkolekcji
end
</pre>
 
Kolekcją może być przedział wartości (to właśnie większość ludzi ma na myśli, gdy mówi o pętli ''for''):
The collection can be a range of values (this is what most people mean when they talk about a for loop):
 
<pre>
Linia 100 ⟶ 101:
</pre>
 
W tym przykładzie przejdziemy również przez kilka elementów tablicy:
In this example we step through some array elements:
 
<pre>
Linia 112 ⟶ 113:
</pre>
 
Ale prześcigamy samych siebie. ''for'' jest tak naprawdę innym sposobem zapisania instrukcji ''each'', która, jak to bywa, jest naszym pierwszym przykładem iteratora. Poniższe formy są równoważne:
But we're getting ahead of ourselves. for is really another way of writing each, which, it so happens, is our first example of an iterator. The following two forms are equivalent:
 
Jeżeli przywykłeś do C lub Javy, możesz preferować tą.
If you're used to C or Java, you might prefer this.
 
<pre>
Linia 122 ⟶ 123:
</pre>
 
Natomiast programista Smalltalka może preferować taką.
A Smalltalk programmer might prefer this.
 
<pre>
Linia 130 ⟶ 131:
</pre>
 
Iteratory często mogą być używane zamiast konwencjonalnych pętli, i jak już nabierzesz wprawy w stosowaniu ich, stają się zazwyczaj łatwiejsze w stosowaniu od pętli. Zatem ruszmy się dalej i dowiedzmy się o nich czegoś więcej.
Iterators can often be substituted for conventional loops, and once you get used to them, they are generally easier to deal with. So let's move on and learn more about them.
<noinclude>
{{ProstaNawigacja|spis=Ruby|poprzart=Ruby/Powrót do prostych przykładów|poprz=Powrót do prostych przykładów|nastart=Ruby/Iteratory|nast=Iteratory}}