S
Sam Kong
Hello!
I don't fully understand symbols in Ruby even if I've read books and
postings here.
Symbols are often compared to strings like the following.
h1 = {:a => 1} #symbol. faster and immutable.
h2 = {'a' => 1}
Ok, I can understand that.
Now, is the term 'symbol' used the same meaning in the following text
(from PickAxe2 page 314)?
<snip>
When Ruby sees a name such as a in an expression, it needs to determine
if it is a local
variable reference or a call to a method with no parameters. To decide
which is the case,
Ruby uses a heuristic. As Ruby parses a source file, it keeps track of
symbols that have
been assigned to. It assumes that these symbols are variables. When it
subsequently
comes across a symbol that could be a variable or a method call, it
checks to see if
it has seen a prior assignment to that symbol. If so, it treats the
symbol as a variable;
otherwise it treats it as a method call. As a somewhat pathological
case of this, consider
the following code fragment, submitted by Clemens Hintze.
</snip>
Before I met Ruby, I regarded 'symbol' as the one in the text.
But Ruby's symbol seems to have its unique meaning and usage.
Can anybody clarify that?
Thanks.
Sam
I don't fully understand symbols in Ruby even if I've read books and
postings here.
Symbols are often compared to strings like the following.
h1 = {:a => 1} #symbol. faster and immutable.
h2 = {'a' => 1}
Ok, I can understand that.
Now, is the term 'symbol' used the same meaning in the following text
(from PickAxe2 page 314)?
<snip>
When Ruby sees a name such as a in an expression, it needs to determine
if it is a local
variable reference or a call to a method with no parameters. To decide
which is the case,
Ruby uses a heuristic. As Ruby parses a source file, it keeps track of
symbols that have
been assigned to. It assumes that these symbols are variables. When it
subsequently
comes across a symbol that could be a variable or a method call, it
checks to see if
it has seen a prior assignment to that symbol. If so, it treats the
symbol as a variable;
otherwise it treats it as a method call. As a somewhat pathological
case of this, consider
the following code fragment, submitted by Clemens Hintze.
</snip>
Before I met Ruby, I regarded 'symbol' as the one in the text.
But Ruby's symbol seems to have its unique meaning and usage.
Can anybody clarify that?
Thanks.
Sam