Forum: Ruby Symbols similiar to defined in c++ ?????

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Servando G. (Guest)
on 2006-03-04 21:31
(Received via mailing list)
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
}
Timothy H. (Guest)
on 2006-03-04 21:49
(Received via mailing list)
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.
Timothy H. (Guest)
on 2006-03-04 22:05
(Received via mailing list)
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.
Logan C. (Guest)
on 2006-03-04 22:50
(Received via mailing list)
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
Servando G. (Guest)
on 2006-03-05 00:54
(Received via mailing list)
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.
Timothy G. (Guest)
on 2006-03-05 01:34
(Received via mailing list)
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.
Anthony DeRobertis (Guest)
on 2006-03-06 17:23
(Received via mailing list)
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
This topic is locked and can not be replied to.