# Re: Constant in Ruby

**URL:** <https://www.ruby-forum.com/t/re-constant-in-ruby/94521>\
**Category:** Ruby\
**Created:** [May 5, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/re-constant-in-ruby/94521 "2006-05-05T18:48:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [May 5, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/re-constant-in-ruby/94521/1 "2006-05-05T18:48:59Z")

</div>

> a # -\> “foo”
> 
> This is true whether you’re dealing with variables or constants.

I think I’m might be missing something here.

In the examples above by just reading the code I would have originally  
expected a to return “foo” in both examples (which it does for me  
running it through irb).

But after thinking about it a little more, if you use gsub! the first  
example does return a #=\> “bar” (just a typo I think)

x = “foo”  
a = x  
x.gsub!(/foo/,“bar”)  
a #=\> “bar”

Please correct me if I am off base here but what I think is happening is  
that when we set a = x what we are really doing is creating a reference  
a to the same object that x points to (which happens to be “foo”). Then  
when we modify the object that x is referencing (changing foo to bar) a  
also changes since it points to the same object.

In the second example when we do x = “bar” we are changing x to point to  
a new object leaving a pointing to the original object “foo”.

I will have to give this some more thought as it has some interesting  
implications. I was originally expecting a to point to a _copy_ of what  
x pointed to not the same object.

–Bill

---

<div class="post-metadata">

**Author:** ![William\_SBillS\_Froe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e0b2c6/32.png) [@William\_SBillS\_Froe](https://www.ruby-forum.com/u/William_SBillS_Froe)\
**Post date:** [May 5, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/re-constant-in-ruby/94521/2 "2006-05-05T18:55:06Z")

</div>

On 5/5/06, William (Bill) Froelich [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think I’m might be missing something here.
> 
> In the examples above by just reading the code I would have originally  
> expected a to return “foo” in both examples (which it does for me  
> running it through irb).
> 
> But after thinking about it a little more, if you use gsub! the first  
> example does return a #=\> “bar” (just a typo I think)

Yes. That’s a typo.

> Please correct me if I am off base here but what I think is happening is  
> that when we set a = x what we are really doing is creating a reference  
> a to the same object that x points to (which happens to be “foo”). Then  
> when we modify the object that x is referencing (changing foo to bar) a  
> also changes since it points to the same object.

Yes, but that’s overthinking it. Jim W.'s “Shoeboxes and  
Bindings” article[1] helps here.

Variables are labels. Constants are just special variables. Variables  
in Ruby are different than they are in any other language I’ve used.  
Variables in Ruby are pure references and do not represent any storage  
space in and of themselves. Variables are simply “the name by which we  
operate on an object”. So when I do:

x = “bar”  
a = x

I have said that the particular instance of string “bar” is able to be  
referred to by the name x. And then I’ve said that the object referred  
to by the name x can be referred to by the name a. So now we have one  
object with two names.

If you want a copy, you have to either #dup or #clone the object.

The object is the atomic element here.

-austin  
[1] [http://onestepback.org/index.cgi/2003/12/15](http://onestepback.org/index.cgi/2003/12/15)
