# Symbols similiar to defined in c++?

**URL:** <https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742>\
**Category:** Ruby\
**Created:** [March 4, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742 "2006-03-04T20:31:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 4, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/1 "2006-03-04T20:31:43Z")

</div>

would it be safe to say that I can use symbols in the same manner as a  
defined variable in c++

define n 9  
void main()  
{  
for (index=0; while index \< n,index++)  
print index  
while  
}

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 4, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/2 "2006-03-04T20:49:57Z")

</div>

Servando G. wrote:

> 

Not really. In C++ you’re assigning a symbolic name to a number. You  
pick the name and you pick the number. In Ruby, all you do is pick the  
name. Ruby picks a number to assign to it. You don’t get to choose, and  
you don’t even get to find out what the number is (at least, without a  
lot of poking around in Ruby internals). Ruby guarantees you that no  
matter where and when you use the symbol in your program, the symbol  
will refer to the identical number. That’s all there is to symbols.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 4, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/3 "2006-03-04T21:05:04Z")

</div>

Tim H. wrote:

> Not really. In C++ you’re assigning a symbolic name to a number. You  
> pick the name and you pick the number. In Ruby, all you do is pick the  
> name. Ruby picks a number to assign to it. You don’t get to choose, and  
> you don’t even get to find out what the number is (at least, without a  
> lot of poking around in Ruby internals). Ruby guarantees you that no  
> matter where and when you use the symbol in your program, the symbol  
> will refer to the identical number. That’s all there is to symbols.

Oops. Forgot. Also, Ruby guarantees that no two different symbols will  
ever be assigned the same number.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 4, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/4 "2006-03-04T21:50:22Z")

</div>

On Mar 4, 2006, at 2:29 PM, Servando G. wrote:

> 

You are looking for a constant (in ruby terms)

N = 9 # Constants must start with a capital letter

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 4, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/5 "2006-03-04T23:54:49Z")

</div>

Logan C. wrote:

> > print index

I am not looking for a constant. I am just trying to understand the uses  
for Symbols. I have been programing for about six years now; and have  
never encountered this symbols concept. I am just trying to understand.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 5, 2006, 12:34am UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/6 "2006-03-05T00:34:04Z")

</div>

A symbol is represented internally as a number (an index in the symbol  
table), but when programming in Ruby this is neither important nor  
visible.

You may have seen in many C libraries how enums are used to represent  
possible states. A number is used internally, but you use the constant  
created in the enum as a label for a possible state or one of a few  
possible options. This makes your program more easily understood and  
much more easily altered.

In Ruby, a symbol is like the constant created in a C enumumeration.  
You can’t control the number assigned, and symbols have no logical  
order, but two symbols with the same name will always be equal within  
any Ruby process. You use them in many of the same places you would use  
an enum. If you want your Warrior class to have the states ‘able’,  
‘injured’, or ‘dead’, you could use the symbols :able, :injured, and  
:dead. These are much faster to compare than the strings ‘able’,  
‘injured’ and ‘dead’ as they are converted internally to a numeric  
representation.

Think of a symbol as a string which is faster to compare with other  
symbols but is slower to convert to a string for display or alteration  
of the associated text. If you need to display something a lot or add  
to the text associated with a symbol, it should probably be a string  
instead. If a string is being used mostly as a label of state, it  
should probably be a symbol instead.

---

<div class="post-metadata">

**Author:** ![Servando\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@Servando\_G](https://www.ruby-forum.com/u/Servando_G)\
**Post date:** [March 6, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/symbols-similiar-to-defined-in-c/76742/7 "2006-03-06T16:23:04Z")

</div>

Tim H. wrote:

> You don’t get to choose,  
> and you don’t even get to find out what the number is (at least,  
> without a lot of poking around in Ruby internals).

Not really â?? finding out the number is easy:

```
    irb(main):001:0> :foo.to_i
    => 15321
```
