Explanation on some code

M

Michael Albers

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.
 
R

Robert Dober

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 =3D {
'some_file.html' =3D> [
'Some text',
'Some more text'
],
'some_other_file.html' =3D> [
'other text',
'And even more text'
],
'yet_another_file.html' =3D> [
['Last text', 2]
]
}

Thanks a lot.
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 ::=3D '{' key_value_pairs '}' | '{' '}';
key_value_pairs ::=3D key "=3D>" value;
key ::=3D value ::=3D expression

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

'a'
{'a' =3D> 42}
x =3D [1,2,3]
y=3D%w{a b c }
h=3D{ :symbol =3D> x, "string" =3D> y}

HTH
Robert


--=20
Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

Robert Dober ;)
 
V

Vladimir Fekete

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
 
S

Sebastian Hungerecker

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top