Symbols similiar to defined in c++ ?????

  • Thread starter Servando Garcia
  • Start date
S

Servando Garcia

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
}
 
L

Logan Capaldo

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
}

You are looking for a constant (in ruby terms)

N = 9 # Constants must start with a capital letter
 
S

Servando Garcia

Logan said:
You are looking for a constant (in ruby terms)

N = 9 # Constants must start with a capital letter
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.
 
T

Timothy Goddard

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.
 
A

Anthony DeRobertis

Tim said:
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 =E2=80=94 finding out the number is easy:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top