# Explanation on some code

**URL:** <https://www.ruby-forum.com/t/explanation-on-some-code/154545>\
**Category:** Ruby\
**Created:** [December 1, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545 "2008-12-01T14:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/1 "2008-12-01T14:39:36Z")

</div>

Hi,

Today i looked at ruby for the first time.

I can’t quite figure out what this code is. Could someone give me an  
explanation.

FILES = {  
‘some\_file.html’ =\> [  
‘Some text’,  
‘Some more text’  
],  
‘some\_other\_file.html’ =\> [  
‘other text’,  
‘And even more text’  
],  
‘yet\_another\_file.html’ =\> [  
[‘Last text’, 2]  
]  
}

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/2 "2008-12-01T15:00:00Z")

</div>

On Mon, Dec 1, 2008 at 2:34 PM, Michael Albers  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## ‘Some more text’ Thanks a lot.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

It is a hash (literally specified at the RHS of the equal sign)  
assigned to a constant, FILES.  
The hash syntax is like the following

hash ::= ‘{’ key\_value\_pairs ‘}’ | ‘{’ ‘}’;  
key\_value\_pairs ::= key “=\>” value;  
key ::= value ::= expression

Your expressions are made of string literals and lists.  
Nough said  
fire up irb and type in some simple expressions like

‘a’  
{‘a’ =\> 42}  
x = [1,2,3]  
y=%w{a b c }  
h={ :symbol =\> x, “string” =\> y}

HTH  
Robert

–  
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. 😉

---

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/3 "2008-12-01T15:13:31Z")

</div>

Hi,

It’s a HASH - code structure similar to array where you use as index  
string  
(and not number). Value of elements in hash are arrays

so for example:

FILES[‘some\_other\_file.html’] will giv you back array:

[‘other text’, ‘And even more text’]

Cheers,

V.

P.S. Google: Ruby pragmatic programmer and find hash section

---

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/4 "2008-12-01T15:22:26Z")

</div>

Thanks for the quick replies. I will figure it out now.

M.

---

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/5 "2008-12-01T19:12:08Z")

</div>

Robert D. wrote:

> hash ::= ‘{’ key\_value\_pairs ‘}’ | ‘{’ ‘}’;  
> key\_value\_pairs ::= key “=\>” value;  
> key ::= value ::= expression

You call it “key\_value\_pair_s_”, but don’t allow more than one pair. I  
think  
the above should read:  
key\_value\_pairs ::= (key\_value\_pair ‘,’)\* key\_value\_pair;  
key\_value\_pair ::= key “=\>” value;

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![mjand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mjand](https://www.ruby-forum.com/u/mjand)\
**Post date:** [December 1, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/explanation-on-some-code/154545/6 "2008-12-01T19:25:38Z")

</div>

U know, exercise left to the reader 😉
