From: Logan Capaldo Date: 2007-08-14T19:10:38+09:00 Subject: Re: what is "dynamic constant assignment" ------=_Part_34893_13803783.1187086240852 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/14/07, Stefano Crocco wrote: > > Alle marted=EC 14 agosto 2007, Nikos Kanellopoulos ha scritto: > > 2007/8/14, sean liu : > > > hi, all > > > > > > today i defined a class like this: > > > class C > > > AB =3D 10 > > > def self.set(value) > > > AB =3D value > > > end > > > end > > > then i got this error: SyntaxError: compile error (irb):10: dynamic > > > constant assignment. > > > Constants can be changed after initialization, but what does this > error > > > mean? > > > Any explanations will be appreciated. > > > > Variable AB is a contant, because it starts with a capital letter. > > You can only assign a contant value (or expression) to it, once. > > That's not true. You can assign a value to a constant more than once, eve= n > if > ruby will issue a warning: > > irb: 001> A =3D 1 > 1 > irb: 002> A =3D 2 > (irb):2: warning: already initialized constant A > 2 > > I guess that the 'dynamic constant assignment' means that you can assing > to a > constant in a method. And the work around is def self.set(value) const_set("AB". value) end Although it might make more sense to do @AB =3D 10 def self.AB @AB end def self.AB=3D(value) @AB =3D value end Stefano > > > > ------=_Part_34893_13803783.1187086240852--