# What is "dynamic constant assignment"

**URL:** <https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881>\
**Category:** ruby-talk\
**Created:** [14 August 2007 08:03 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881 "2007-08-14T08:03:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Liu](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Sean\_Liu](https://rubytalk.org/u/Sean_Liu)\
**Post date:** [14 August 2007 08:03 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/1 "2007-08-14T08:03:05Z")

</div>

hi, all

today i defined a class like this:  
class C  
&nbsp;&nbsp;AB = 10  
&nbsp;&nbsp;def self.set(value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AB = value  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Nikos\_Kanellopoulos](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nikos\_Kanellopoulos](https://rubytalk.org/u/Nikos_Kanellopoulos)\
**Post date:** [14 August 2007 08:23 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/2 "2007-08-14T08:23:37Z")

</div>

Variable AB is a contant, because it starts with a capital letter.  
You can only assign a contant value (or expression) to it, once.

> **···**
>
> 2007/8/14, sean liu \<seanliugm@gmail.com\>:
> 
> > hi, all
> > 
> > today i defined a class like this:  
> > class C  
> > &nbsp;&nbsp;AB = 10  
> > &nbsp;&nbsp;def self.set(value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AB = value  
> > &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [14 August 2007 08:27 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/3 "2007-08-14T08:27:36Z")

</div>

Alle martedì 14 agosto 2007, Nikos Kanellopoulos ha scritto:

> \> 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.
> 
> 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

> **···**
>
> > 2007/8/14, sean liu \<seanliugm@gmail.com\>:

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [14 August 2007 10:10 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/4 "2007-08-14T10:10:38Z")

</div>

> Alle martedì 14 agosto 2007, Nikos Kanellopoulos ha scritto:  
> \> \> 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.  
> \>  
> \> 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.

And the work around is  
def self.set(value)  
&nbsp;&nbsp;const\_set("AB". value)  
end

Although it might make more sense to do

@AB = 10  
def self.AB  
&nbsp;&nbsp;@AB  
end

def self.AB=(value)  
&nbsp;&nbsp;@AB = value  
end

Stefano

> **···**
>
> On 8/14/07, Stefano Crocco \<stefano.crocco@alice.it\> wrote:
> 
> > \> 2007/8/14, sean liu \<seanliugm@gmail.com\>:

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [14 August 2007 10:15 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/5 "2007-08-14T10:15:32Z")

</div>

"Any explanations will be appreciated."

Ruby does not allow redefinitions of constants  
in a method.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [26 September 2008 17:03 UTC](https://rubytalk.org/t/what-is-dynamic-constant-assignment/39881/6 "2008-09-26T17:03:00Z")

</div>

> > 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)  
> &nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
