# A question about ruby array

**URL:** <https://www.ruby-forum.com/t/a-question-about-ruby-array/155822>\
**Category:** Ruby\
**Created:** [December 17, 2008, 8:43am UTC](https://www.ruby-forum.com/t/a-question-about-ruby-array/155822 "2008-12-17T08:43:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 8:43am UTC](https://www.ruby-forum.com/t/a-question-about-ruby-array/155822/1 "2008-12-17T08:43:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 9:48am UTC](https://www.ruby-forum.com/t/a-question-about-ruby-array/155822/2 "2008-12-17T09:48:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 17, 2008, 11:18am UTC](https://www.ruby-forum.com/t/a-question-about-ruby-array/155822/3 "2008-12-17T11:18:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [December 18, 2008, 2:03am UTC](https://www.ruby-forum.com/t/a-question-about-ruby-array/155822/4 "2008-12-18T02:03:23Z")

</div>

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.
