Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

Symbols vs. Strings


I'm new to Ruby, but coming up to speed quickly.  One question I still
have never seen a good explanation to is this: When is it preferred to a
key a hash with a symbol, and when is it keyed by string?  Is this just
personal preference, or is there a rule of thumb?

For example, in the Rails book, the session variable is always populated
with symbols, i.e.:

session[:user] = User.new

It's also obviously completely common throughout the Rails framework
(e.g., :controller =>, :action =>, etc.)

So, when should I use what...or what should I prefer?

--
Posted via http://www.ruby-forum.com/.

John Blanco wrote:
> I'm new to Ruby, but coming up to speed quickly.  One question I still
> have never seen a good explanation to is this: When is it preferred to a
> key a hash with a symbol, and when is it keyed by string?  Is this just
> personal preference, or is there a rule of thumb?

> For example, in the Rails book, the session variable is always populated
> with symbols, i.e.:

> session[:user] = User.new

> It's also obviously completely common throughout the Rails framework
> (e.g., :controller =>, :action =>, etc.)

> So, when should I use what...or what should I prefer?

This question comes up a lot. You can search the list archives for
comprehensive discussions. Try
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/7e9a4...

--
Posted via http://www.ruby-forum.com/.

On 5/29/07, John Blanco <zabl@yahoo.com> wrote:

> I'm new to Ruby, but coming up to speed quickly.  One question I still
> have never seen a good explanation to is this: When is it preferred to a
> key a hash with a symbol, and when is it keyed by string?  Is this just
> personal preference, or is there a rule of thumb?

> For example, in the Rails book, the session variable is always populated
> with symbols, i.e.:

> session[:user] = User.new

> It's also obviously completely common throughout the Rails framework
> (e.g., :controller =>, :action =>, etc.)

> So, when should I use what...or what should I prefer?

Symbols are ligh-weight, they don't have so much methods to initialize
has strings:

 irb(main):009:0> :asd.methods.size
=> 45
irb(main):010:0> "asd".methods.size
=> 143

So thats why you use it in params or other places when you just need
the name. Also a difference is that a symbol :sym is :sym everywhere,
but a string "string" -only the string, not assigned to a variable- is
a different refence to a String object  each time you write "string" .

Those are the most important diferences IMO. Sorry if i wasn't that clear tho.

Cheers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

One thing to keep in mind is that, as far as I know, symbols are  
never garbage collected, so their excessive/inappropriate use might  
introduce risk of memory leaks.

George

On 30 May 2007, at 00:06, Dave Grijalva wrote:

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFGXTZcuHSlGoVGf7URAqhKAKC28rsjkJ7tv2f1rxsdidLachrB6QCgs50D
qqsnSiEX19rHfPqzA9z9/DI=
=tKg1
-----END PGP SIGNATURE-----

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc