[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Servando Garcia

3/4/2006 6:26:00 PM

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



5 Answers

Rudolfs Osins

3/4/2006 6:57:00 PM

0

Check out these pages:http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbolshttp://www.oreillynet.com/ruby/blog/2005/12/digging_into_ruby_symbol... 3/4/06, Servando Garcia <garcia.servando@gmail.com> wrote:> 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>>>

Gary Wright

3/4/2006 7:21:00 PM

0


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





Meinrad Recheis

3/4/2006 7:23:00 PM

0

On 3/4/06, Servando Garcia <garcia.servando@gmail.com> wrote:
>
> 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 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

zimba.tm@gmail.com

3/5/2006 2:42:00 PM

0

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

James McCarthy

3/6/2006 9:18:00 PM

0

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?