Create_table

“The Rails Way” book mentions the following regarding the create_table
method:

Why do we specify identifiers with symbols instead ofstrings? Both will
work, but symbols require one less keystroke.

What is meant by: “But symbols require one less keystroke?”

Thanks.

On 15 July 2010 09:38, Abder-Rahman A. [email protected] wrote:

“The Rails Way” book mentions the following regarding the create_table
method:

Why do we specify identifiers with symbols instead ofstrings? Both will
work, but symbols require one less keystroke.

What is meant by: “But symbols require one less keystroke?”

“string”
:string

one has two quote marks; the other only one colon.

But “less keystrokes” is a glib argument in favour of symbols… a
better reason for using symbols over strings is that symbols are more
efficient in memory. They act like pointers to a single string, where
quoted-strings all exist individually in memory, so if you use the
same string a lot, you use lots more memory than if you use the same
symbol a lot.

Michael P. wrote:

On 15 July 2010 09:38, Abder-Rahman A. [email protected] wrote:

“The Rails Way” book mentions the following regarding the create_table
method:

Why do we specify identifiers with symbols instead ofstrings? Both will
work, but symbols require one less keystroke.

What is meant by: “But symbols require one less keystroke?”

“string”
:string

one has two quote marks; the other only one colon.

But “less keystrokes” is a glib argument in favour of symbols… a
better reason for using symbols over strings is that symbols are more
efficient in memory. They act like pointers to a single string, where
quoted-strings all exist individually in memory, so if you use the
same string a lot, you use lots more memory than if you use the same
symbol a lot.

Thanks for your reply.

Regarding memory usage. If the symbol is acting like a pointer and I use
it a lot, doesn’t that mean I’m using the string the symbol is pointing
to, and thus using memory? I didn’t really get the memory benefit of
symbols over strings if that can be described a bit.

Thanks.

On Jul 15, 7:24 pm, Abder-Rahman A. [email protected] wrote:

Regarding memory usage. If the symbol is acting like a pointer and I use
it a lot, doesn’t that mean I’m using the string the symbol is pointing
to, and thus using memory? I didn’t really get the memory benefit of
symbols over strings if that can be described a bit.

The difference is that a given symbol only exists in memory once
whereas you could have many copies of an identical string floating
around, each using however many bytes. The flipside is that symbols
are never released. See also
http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pretty-strings

Fred

Frederick C. wrote:

On Jul 15, 7:24�pm, Abder-Rahman A. [email protected] wrote:

Regarding memory usage. If the symbol is acting like a pointer and I use
it a lot, doesn’t that mean I’m using the string the symbol is pointing
to, and thus using memory? I didn’t really get the memory benefit of
symbols over strings if that can be described a bit.

The difference is that a given symbol only exists in memory once
whereas you could have many copies of an identical string floating
around, each using however many bytes. The flipside is that symbols
are never released. See also
has_many :through - Symbols are not pretty strings

Fred

Thanks a lot.