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
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
···
Le 10/4/2005, "Glenn Smith" <glenn.ruby@gmail.com> a écrit:
--
No-one expects the Solaris POSIX implementation!
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:
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
···
From: "Glenn Smith" <glenn.ruby@gmail.com>
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.
···
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.
--
Nicholas Seckar aka. Ulysses
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