Symbols

  • Thread starter Servando Garcia
  • Start date
S

Servando Garcia

Hello all .
I am a newbie to Ruby. I have been reading everything I can get my
hands on about Ruby. I understand what a symbol is and how it saves
memory space. I just can not seem to find any good reason to use one. I
am sure I am missing something. Would some one please show me a good
working example for using a symbol, please not the "Foo Bar" example again.


Sam
 
R

Rudolfs Osins

Q2hlY2sgb3V0IHRoZXNlIHBhZ2VzOgpodHRwOi8vZ2x1LnR0b25vLnVzL2FydGljbGVzLzIwMDUv
MDgvMTkvdW5kZXJzdGFuZGluZy1ydWJ5LXN5bWJvbHMKaHR0cDovL3d3dy5vcmVpbGx5bmV0LmNv
bS9ydWJ5L2Jsb2cvMjAwNS8xMi9kaWdnaW5nX2ludG9fcnVieV9zeW1ib2xzXzEuaHRtbAoKCgpP
biAzLzQvMDYsIFNlcnZhbmRvIEdhcmNpYSA8Z2FyY2lhLnNlcnZhbmRvQGdtYWlsLmNvbT4gd3Jv
dGU6Cj4gSGVsbG8gYWxsIC4KPiAgICAgSSBhbSBhIG5ld2JpZSB0byBSdWJ5LiBJIGhhdmUgYmVl
biByZWFkaW5nIGV2ZXJ5dGhpbmcgSSBjYW4gZ2V0IG15Cj4gaGFuZHMgb24gYWJvdXQgUnVieS4g
SSB1bmRlcnN0YW5kIHdoYXQgYSBzeW1ib2wgaXMgYW5kIGhvdyBpdCBzYXZlcwo+IG1lbW9yeSBz
cGFjZS4gSSBqdXN0IGNhbiBub3Qgc2VlbSB0byBmaW5kIGFueSBnb29kIHJlYXNvbiB0byB1c2Ug
b25lLiBJCj4gYW0gc3VyZSBJIGFtIG1pc3Npbmcgc29tZXRoaW5nLiBXb3VsZCBzb21lIG9uZSBw
bGVhc2Ugc2hvdyBtZSBhIGdvb2QKPiB3b3JraW5nIGV4YW1wbGUgZm9yIHVzaW5nIGEgc3ltYm9s
LCBwbGVhc2Ugbm90IHRoZSAiRm9vIEJhciIgZXhhbXBsZSBhZ2Fpbi4KPgo+Cj4gU2FtCj4KPgo+
Cg==
 
G

gwtmp01

I am a newbie to Ruby. I have been reading everything I can get
my hands on about Ruby. I understand what a symbol is and how it
saves memory space. I just can not seem to find any good reason to
use one. I am sure I am missing something. Would some one please
show me a good working example for using a symbol, please not the
"Foo Bar" example again.

IMHO, when your goal is to model distinct values but the actual bit
patterns
are irrelevant then symbols are often the best approach. When you
goal is
to model a particular sequence of bytes or characters then strings
are often
the best solution, especially when the sequence might change over time.

For example, if you want to record 'gender' you could model that in lots
of different ways:

male female
---- ------
0 1
1 0
'm' 'f'
'male' 'female'
'mars' 'venus'
-1 1
1 -1
true false
false true
:male :female

In fact, the actual bit pattern is not important. What is important
is that you have two unique objects and that they can be clearly mapped
to some external representation of male and female. Symbols are
perfect for
this because they implement value semantics (equality is based on
identity)
and they also internalize the mapping to external strings. If you
choose
to use 1 and 0 instead you would still have to have some sort of
conversion
table or code routine that mapped 1 to 'female' and 0 to 'male' (or vice
versa). Use symbols and you get the conversion for 'free'.


Gary Wright
 
M

Meinrad Recheis

------=_Part_8440_83618.1141500154889
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello all .
I am a newbie to Ruby. I have been reading everything I can get my
hands on about Ruby. I understand what a symbol is and how it saves
memory space. I just can not seem to find any good reason to use one. I
am sure I am missing something. Would some one please show me a good
working example for using a symbol, please not the "Foo Bar" example
again.


Sam

hi Sam.
symbols are a coding style instrument:

personally, i use symbols whereever i want to refer to method names, as key=
s
in hashes or as other constant identifiers.

symbols as constant identifiers that need not be initialized are nicer than
code like this:

LAYOUT_CONSTANTS=3D[
LAYOUT_RIGHT=3D0,
LAYOUT_LEFT=3D1,
...
]

or even:

LAYOUT_CONSTANTS=3D[
LAYOUT_RIGHT=3D"LAYOUT_RIGHT",
LAYOUT_LEFT=3D"LAYOUT_LEFT",
...
]

code snippets that illustrate my coding style using symbols:
if object.respond_to? :a_method

{ :key =3D> "value" }

subject.do_something( sideeffect=3D:notify)

you could use strings instead, but this kind of coding style enhances code
readability, because if you see a symbol you automatically associate it wit=
h
methods, variables, and constant expressions and the symbol syntax
differentiates these from normal (data-)strings.
i think i don't need to tell you guys why good coding styles are important
in software engineering :)
-- henon

------=_Part_8440_83618.1141500154889--
 
W

William James

For example, if you want to record 'gender' you could model that in lots
of different ways:

male female
---- ------
0 1
1 0
'm' 'f'
'male' 'female'
'mars' 'venus'
-1 1
1 -1
true false
false true
:male :female

Nobody who has any sense wants to record the "gender" of
a human being. A person is a member of a certain sex, not
of a "gender". Hence, "the weaker sex". Only words have
gender. That is elementary. Unfortunately, ignorant witlings
have decided that it is trendy and politically correct to
substitute 'gender' for 'sex'.
 
C

chiaro scuro

Nobody who has any sense wants to record the "gender" of
a human being. A person is a member of a certain sex, not
of a "gender". Hence, "the weaker sex". Only words have
gender. That is elementary. Unfortunately, ignorant witlings
have decided that it is trendy and politically correct to
substitute 'gender' for 'sex'.

It must have started with people filling in the 'sex' entry in forms
with YES :)
 
G

gwtmp01

Nobody who has any sense wants to record the "gender" of
a human being. A person is a member of a certain sex, not
of a "gender". Hence, "the weaker sex". Only words have
gender. That is elementary. Unfortunately, ignorant witlings
have decided that it is trendy and politically correct to
substitute 'gender' for 'sex'.

Thanks for pointing all that out. Here is a corrected version
of my table:

ignorant educated
-------- --------
0 1
'g' 'w'
'gary' 'william'
true false
:gary :william

Gary 'witless' Wright
 
Z

zimbatm

Also, symbols can be used when the string is not intended to change.

Consider this :

position = :west
position.gsub!(/west/, 'south')
=> NoMethodError

Cheers,
zimba.tm
 
A

A LeDonne

Nobody who has any sense wants to record the "gender" of
a human being. A person is a member of a certain sex, not
of a "gender". Hence, "the weaker sex". Only words have
gender. That is elementary. Unfortunately, ignorant witlings
have decided that it is trendy and politically correct to
substitute 'gender' for 'sex'.

One important plus to using symbols... it's obvious how to then extend
the system when you realize you need to add :intersex, as biological
sex is not always an either/or proposition.
 
J

James McCarthy

what about using symbols to represent methods or instances of classes
for example:
myclass:
instance_class:

Just using it as shorthand for classes or methods?
 

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,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top