Forum: Ruby Symbols

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.
9c7e3e773ac08cb6e7ae2f4f7eeadaa6?d=identicon&s=25 Servando Garcia (Guest)
on 2006-03-04 19:27
(Received via mailing list)
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
66e5b65d1fe0183ca04053fda407a185?d=identicon&s=25 Rudolfs Osins (Guest)
on 2006-03-04 19:58
(Received via mailing list)
D8fb06dfc08a477ecb0a76ffdbff3475?d=identicon&s=25 Chiaro Scuro (chiaroscuro)
on 2006-03-04 20:10
(Received via mailing list)
I usually use a symbol instead of a string when I use it to represent
a 'concept' meaningful to the program, rather than just some text that
is meaningful only to the user.

For example, what I used to model as 'flags' or 'options' in other
languages become symbols in ruby.

i.e: "ciao".translate_to :english

"ciao" is a piece of text meaningful to the user.  :english is a
symbol meaningful to the program.

When the concept that I represent as a symbol becomes more complex I
usually evolve it to an object.

On 3/4/06, Servando Garcia <garcia.servando@gmail.com> wrote:
>
>


--

-- Chiaroscuro --
Liquid Development Blog:
http://feeds.feedburner.com/blogspot/liquiddevelopment
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-03-04 20:22
(Received via mailing list)
On Mar 4, 2006, at 1:26 PM, Servando Garcia wrote:
>    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
48d1aca7191f2d16e184971054c7c143?d=identicon&s=25 Meinrad Recheis (Guest)
on 2006-03-04 20:25
(Received via mailing list)
On 3/4/06, Servando Garcia <garcia.servando@gmail.com> wrote:
> Sam
>

hi Sam.
symbols are a coding style instrument:

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

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

LAYOUT_CONSTANTS=[
  LAYOUT_RIGHT=0,
  LAYOUT_LEFT=1,
...
]

or even:

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

code snippets that illustrate my coding style using symbols:

> if object.respond_to? :a_method
>
> { :key => "value" }
>
> subject.do_something( sideeffect=: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
with
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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-04 21:56
(Received via mailing list)
gwtmp01@mac.com wrote:

> 	-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'.
D8fb06dfc08a477ecb0a76ffdbff3475?d=identicon&s=25 Chiaro Scuro (chiaroscuro)
on 2006-03-04 22:14
(Received via mailing list)
On 3/4/06, William James <w_a_x_man@yahoo.com> wrote:
> 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 :-)
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-03-04 23:15
(Received via mailing list)
On Mar 4, 2006, at 3:53 PM, William James wrote:
> 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
0bf205083975ef054b4ad08915d1d653?d=identicon&s=25 zimbatm (Guest)
on 2006-03-05 15:43
(Received via mailing list)
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
05e48e632fdd0b2c25d27042f52c11d5?d=identicon&s=25 A LeDonne (Guest)
on 2006-03-06 21:21
(Received via mailing list)
On 3/4/06, William James <w_a_x_man@yahoo.com> wrote:
> >       'male'  'female'
> 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.
57641b9ad0bb552553b41a71d6680362?d=identicon&s=25 James McCarthy (Guest)
on 2006-03-06 22:20
(Received via mailing list)
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?
This topic is locked and can not be replied to.