Ruby/Stałe klasowe: 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:
== Stałe klasowe ==
A constant has a name starting with an uppercase character. It should be assigned a value at most once. In the current implementation of ruby, reassignment of a constant generates a warning but not an error (the non-ANSI version of eval.rb does not report the warning):
 
Nazwa stałej zaczyna się od dużej litery. Powinna jej być przypisana wartość, najwyżej jeden raz. W bieżącej implementacji Rubiego ponowne przypisanie wartości do stałej generuje ostrzeżenie, ale nie błąd (wersja nie-ANSI programu ''eval.rb'' nie pokaże ostrzeżenia):
 
<pre>
ruby>fluid=30
30
Linia 10 ⟶ 13:
(eval):1: warning: already initialized constant Solid
33
</pre>
 
Stałe mogą być definiowane wewnątrz klas, ale w przeciwieństwie do zmiennych instancji, są one dostępne na zewnątrz klasy.
Constants may be defined within classes, but unlike instance variables, they are accessible outside the class.
 
<pre>
ruby> class ConstClass
| C1=101
Linia 29 ⟶ 34:
101 102 103
nil
</pre>
 
Stałe mogą być również definiowane w modułach.
Constants can also be defined in modules.
 
<pre>
ruby> module ConstModule
| C1=101
Linia 61 ⟶ 68:
ruby> ConstModule::C1 # "enough rope to shoot yourself in the foot"
99
</pre>
<noinclude>
{{ProstaNawigacja|spis=Ruby|poprzart=Ruby/Zmienne lokalne|poprz=Zmienne lokalne|nastart=Ruby/Przetwarzanie wyjątków: rescue|nast=Przetwarzanie wyjątków: rescue}}