Discussion: Newbie help
Afficher un message
Vieux 16/09/2007, 01h45   #2
Summercool
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie

On 9/15/07, Ali Koubeissi <ali.koubei...@hotmail.com> wrote:

> Hey, I've started with Ruby two days ago, and I have some questions.


> 1- What's the use of Symbols? I've read that symbols are (Symbols are
> lightweight strings. Usually, symbols are used in situations where you
> need a string but you won't be printing it to the screen.), however I
> cannot understand how to use them, and so I tried to read some examples
> and I got this:


you can think of symbol as string... except symbol is always identical
if it
has the same name... two strings "title" and "title" have different
values
(the pointer to the memory location)... and you need to see the
content to
tell what the value is.

i think you can think of it as:

when Ruby see a new symbol, it gives it an ID of 1
then next time it sees a different label, it gives it an ID of 2

so when a symbol is used as a hash key, it is just using "1" as the ID
instead of a string... which will save the time of processing the
string and
the program run faster...

although internally, i wonder whether it is 1, 2, 3, 4, etc, or the
pointer
to the symbol object like 0x32fe117F which is still a unique ID....

  Réponse avec citation
 
Page generated in 0,05328 seconds with 9 queries