[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Symbols...

Glenn Smith

4/10/2005 6:05:00 PM

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?

--

All the best
Glenn
Aylesbury, UK


5 Answers

ES

4/10/2005 6:18:00 PM

0


Le 10/4/2005, "Glenn Smith" <glenn.ruby@gmail.com> a écrit:
>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.

>Help?

>Glenn

E

--
No-one expects the Solaris POSIX implementation!



Glenn Smith

4/10/2005 7:08:00 PM

0

Perfect! Thanks muchly

Glenn


On Apr 10, 2005 7:18 PM, Saynatkari <ruby-ml@magical-cat.org> wrote:
>
> Le 10/4/2005, "Glenn Smith" <glenn.ruby@gmail.com> a écrit:
> >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.
>
> >Help?
>
> >Glenn
>
> E
>
> --
> No-one expects the Solaris POSIX implementation!
>
>


--

All the best
Glenn
Aylesbury, UK



Bill Kelly

4/10/2005 7:16:00 PM

0

From: "Glenn Smith" <glenn.ruby@gmail.com>
>
> Nope, still not entirely sure I *get* symbols.

More info:
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...

> 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:

>> val = :abcdefghijklmnopqrstuvwxyz
=> :abcdefghijklmnopqrstuvwxyz
>> t1 = Time.now; 1_000_000.times { val == :abcdefghijklmnopqrstuvwxyz }; Time.now - t1
=> 0.671

vs. Strings:

>> val = "abcdefghijklmnopqrstuvwxyz"
=> "abcdefghijklmnopqrstuvwxyz"
>> t1 = Time.now; 1_000_000.times { val == "abcdefghijklmnopqrstuvwxyz" }; Time.now - t1
=> 2.283


Regards,

Bill




Nicholas Seckar

4/10/2005 8:06:00 PM

0

On Sunday 10 April 2005 15:16, Bill Kelly wrote:
> 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.

--

Nicholas Seckar aka. Ulysses


Glenn Smith

4/11/2005 10:56:00 AM

0

Every day I scratch a little bit more off the surface of Ruby!

Thanks guys!!


On Apr 10, 2005 9:05 PM, Nicholas Seckar <nseckar@gmail.com> wrote:
> On Sunday 10 April 2005 15:16, Bill Kelly wrote:
> > 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.
>
> --
>
> Nicholas Seckar aka. Ulysses
>
>


--

All the best
Glenn
Aylesbury, UK