Symbols similiar to defined in c++?


#1

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
}


#2

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.


#3

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.


#4

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


#5

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.


#6

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.


#7

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