Symbols...

G

Glenn Smith

Nope, still not entirely sure I *get* symbols.

I'm writing stuff in rails at the moment, and *seem* to have free
choice as to whether to use something like:

@params[:year]

or

@params['year']

What's the difference? Coming from mainly a VB and PL/SQL (and rarely
'C') I'm not sure there is an equivalent in these languages? Just not
sure I get them...

Help?
 
S

Saynatkari

Le 10/4/2005 said:
Nope, still not entirely sure I *get* symbols.

I'm writing stuff in rails at the moment, and *seem* to have free
choice as to whether to use something like:

@params[:year]

or

@params['year']

What's the difference? Coming from mainly a VB and PL/SQL (and rarely
'C') I'm not sure there is an equivalent in these languages? Just not
sure I get them...

Symbols are immutable, unlike Strings. The biggest difference,
however, is the conceptual one. Wherever you need to use a
descriptive value for a variable (basically wherever you would
use a string in other languages) you have the choice of using
a String or a Symbol; for you, the programmer, both can convey
immediate information about the value :)flag_set or 'flag set'
is clearer than 1) and they are especially useful as Hash keys.

Symbols are slightly more lightweight than Strings and, along
with them being the 'Ruby Way', that is the main reason to use
them.

My rule of thumb is to use Symbols anytime a string representation
is needed but I will not need to modify the string or output
it to a user.

E
 
G

Glenn Smith

Perfect! Thanks muchly

Glenn


Le 10/4/2005 said:
Nope, still not entirely sure I *get* symbols.

I'm writing stuff in rails at the moment, and *seem* to have free
choice as to whether to use something like:

@params[:year]

or

@params['year']

What's the difference? Coming from mainly a VB and PL/SQL (and rarely
'C') I'm not sure there is an equivalent in these languages? Just not
sure I get them...

Symbols are immutable, unlike Strings. The biggest difference,
however, is the conceptual one. Wherever you need to use a
descriptive value for a variable (basically wherever you would
use a string in other languages) you have the choice of using
a String or a Symbol; for you, the programmer, both can convey
immediate information about the value :)flag_set or 'flag set'
is clearer than 1) and they are especially useful as Hash keys.

Symbols are slightly more lightweight than Strings and, along
with them being the 'Ruby Way', that is the main reason to use
them.

My rule of thumb is to use Symbols anytime a string representation
is needed but I will not need to modify the string or output
it to a user.

E
 
B

Bill Kelly

From: "Glenn Smith said:
Nope, still not entirely sure I *get* symbols.

More info:
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/113107
I'm writing stuff in rails at the moment, and *seem* to have free
choice as to whether to use something like:

@params[:year]

or

@params['year']

What's the difference?

In the above usage, I'd guess the difference is "not much", or
"one fewer character typed". :)

Probably (just a guess) the @params behind the scenes is a
hash that has keys of Strings not Symbols. If this is so, then
in the above, :year is being converted, probably, with .to_s
before being used as a lookup in @params hash anyway.

So from the programmer's point of view, in the above situation,
the use of the symbol probably just comes down to aesthetics.

But, here's an example showing the speediness of symbol-to-
symbol comparisons:
=> 0.671

vs. Strings:
=> 2.283


Regards,

Bill
 
N

Nicholas Seckar

Probably (just a guess) the @params behind the scenes is a
hash that has keys of Strings not Symbols. If this is so, then
in the above, :year is being converted, probably, with .to_s
before being used as a lookup in @params hash anyway.

You are exactly correct. (The class is called HashWithIndifferentAccess..)

The main reason for using strings as keys for indifferent hashes is the fact
that symbols are not garbage collected. If symbols were used, then
constructing indifferent hashes with arbitrary keys would (eventually)
produce the symptoms of a memory leak.
 

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
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top