Forum: Ruby what is "dynamic constant assignment"

33caac317433721964f817e6cb5d2608?d=identicon&s=25 sean liu (Guest)
on 2007-08-14 10:04
(Received via mailing list)
hi, all

today i defined a class like this:
class C
  AB = 10
  def self.set(value)
     AB = 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.
3a52e328d93985fff7d293f640ad3650?d=identicon&s=25 Nikos Kanellopoulos (Guest)
on 2007-08-14 10:25
(Received via mailing list)
2007/8/14, sean liu <seanliugm@gmail.com>:
> 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.
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-08-14 10:29
(Received via mailing list)
Alle martedì 14 agosto 2007, Nikos Kanellopoulos ha scritto:
> > 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,
even if
ruby will issue a warning:

irb: 001> A = 1
1
irb: 002> A = 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.

Stefano
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-08-14 12:11
(Received via mailing list)
On 8/14/07, Stefano Crocco <stefano.crocco@alice.it> wrote:
> > >   end
>
> 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 = 10
def self.AB
  @AB
end

def self.AB=(value)
  @AB = value
end



Stefano
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2007-08-14 12:15
"Any explanations will be appreciated."

Ruby does not allow redefinitions of constants
in a method.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2008-09-26 19:03
>> 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

Or, if you aren't in a class method

def set(value)
 self.class.const_set(:AB, value) # can use a symbol
end


Cheers.
-=R
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.