# Constant in Ruby

**URL:** <https://rubytalk.org/t/constant-in-ruby/27050>\
**Category:** ruby-talk\
**Created:** [30 April 2006 08:06 UTC](https://rubytalk.org/t/constant-in-ruby/27050 "2006-04-30T08:06:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_Bystritsky1](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@Gennady\_Bystritsky1](https://rubytalk.org/u/Gennady_Bystritsky1)\
**Post date:** [30 April 2006 08:06 UTC](https://rubytalk.org/t/constant-in-ruby/27050/1 "2006-04-30T08:06:11Z")

</div>

Ruby issues a warning when assigning to already initialized constant.  
Unless, of course, you run it with -W0 option.

Gennady.

> **···**
>
> > -----Original Message-----  
> > From: list-bounce@example.com  
> > [mailto:list-bounce@example.com] On Behalf Of Charlie  
> > Sent: Saturday, April 29, 2006 23:58  
> > To: ruby-talk ML  
> > Subject: Re: Constant in Ruby.
> > 
> > Mike Fletcher wrote:  
> > \> Charlie wrote:  
> > \>\> I'm new to Ruby programming and I saw this article which  
> > concerns me:  
> > \>\>  
> > \>\>  
> > [http://www.bitwisemag.com/copy/features/opinion/ruby/ruby\_debate.html](http://www.bitwisemag.com/copy/features/opinion/ruby/ruby_debate.html)  
> > \>\>  
> > \>\> Don't get me wrong, I love Ruby. But, the idea that a  
> > constant is not  
> > \>\> a constant is worrying isn't it? Or have I missed  
> > something? Please  
> > \>\> comment.  
> > \>\>  
> > \>\> X = 10  
> > \>\> Y = "hello world"  
> > \>\> Z = "hello world"  
> > \>\>  
> > \>\> X = 20  
> > \>\> Y = "byebye" \<\< "abc"  
> > \>\> myvar = Z \<\< "xyz"  
> > \>\>  
> > \>\> puts(X)  
> > \>\> 20  
> > \>\> puts(Y)  
> > \>\> byebyeabc  
> > \>\> puts(Z)  
> > \>\> hello worldxyz  
> > \>\> myvar  
> > \>\> "hello worldxyz"  
> > \>  
> > \> It's constant in that you can't reassign a different  
> > instance to the  
> > \> same name (try setting X = 10 after you've set it to 20 and  
> > Ruby will  
> > \> gripe). The instances themselves may still be mutable (see  
> > \> Object#freeze if you want to change that).
> > 
> > okay, I just try this:
> > 
> > X = 20  
> > X = 10  
> > puts(X)  
> > 10  
> > X.freeze  
> > X = 30  
> > puts(X)  
> > 30
> > 
> > &nbsp;&nbsp;&nbsp;Sorry, but I find it difficult to understand as I'm new to  
> > it. Would be helpful if you can enlighten me with some  
> > examples. Thanks.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
