Forum: Ruby A question about ruby array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-12-17 08:43
I read some ruby examples and found there is a syntax I don't
understand. When getting an array element, it uses "array[:id]". I
wander why there is a colon before the index. What does this mean?
7864d809993744583d15b0f0a2f8dded?d=identicon&s=25 Marius Žilėnas (kronidas)
on 2008-12-17 09:48
Hello,
there are Hashes and Arrays. They are different.

array[:id] is for accessing a value from a Hash by using :id as a key.

array = {:id => 1, :something => "value"}
puts array[:id] #prints 1
puts array[:something] #prints "value"

Your example uses wrong name for a variable. It should be "hash" :).

Marius Žilėnas

Zhao Yi wrote:
> I read some ruby examples and found there is a syntax I don't
> understand. When getting an array element, it uses "array[:id]". I
> wander why there is a colon before the index. What does this mean?
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-17 11:18
(Received via mailing list)
Hi --

On Wed, 17 Dec 2008, Zhao Yi wrote:

> I read some ruby examples and found there is a syntax I don't
> understand. When getting an array element, it uses "array[:id]". I
> wander why there is a colon before the index. What does this mean?

It's actually hashes, not arrays, that use this syntax (see other
reply in this thread). What the :id thing means is that :id is a
Symbol object. Symbol is a class of objects that correspond directly
to entries in Ruby's internal symbol table. There's one entry for
every identifier in use while your program is running: every variable
name, method name, and constant. If you want to see them all, you can
do:

   Symbol.all_symbols

in irb.

The symbol table is really part of the inner workings of the
interpreter, but Ruby exposes it to programmer-space through the
Symbol class. Symbols have characters; therefore, they're used a lot
in situations where you might also use strings (such as hash keys).
They're more lightweight in terms of processing than strings are: a
string has to know how to resize itself, for example, whereas a symbol
is immutable.


David
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-12-18 02:03
David A. Black wrote:
> It's actually hashes, not arrays, that use this syntax (see other
> reply in this thread). What the :id thing means is that :id is a
> Symbol object. Symbol is a class of objects that correspond directly
> to entries in Ruby's internal symbol table. There's one entry for
> every identifier in use while your program is running: every variable
> name, method name, and constant. If you want to see them all, you can
> do:
>
>    Symbol.all_symbols
>
> in irb.
>
> The symbol table is really part of the inner workings of the
> interpreter, but Ruby exposes it to programmer-space through the
> Symbol class. Symbols have characters; therefore, they're used a lot
> in situations where you might also use strings (such as hash keys).
> They're more lightweight in terms of processing than strings are: a
> string has to know how to resize itself, for example, whereas a symbol
> is immutable.
>
>
> David

Ok I understand,

thanks for your explanation.
This topic is locked and can not be replied to.