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.
···
--
Posted via http://www.ruby-forum.com/ .
Forum
(Forum)
1 December 2008 13:53
2
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
···
On Mon, Dec 1, 2008 at 2:34 PM, Michael Albers <registraties@concatenate.nl> wrote:
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.
--
Posted via http://www.ruby-forum.com/\ .
--
Ne baisse jamais la tête, tu ne verrais plus les étoiles.
Robert Dober
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
···
On Mon, Dec 01, 2008 at 10:34:08PM +0900, Michael Albers wrote:
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.
--
Posted via http://www.ruby-forum.com/\ .
Thanks for the quick replies. I will figure it out now.
M.
···
--
Posted via http://www.ruby-forum.com/ .
Robert Dober 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
···
--
NP: Tyr - Hail to the Hammer
Jabber: sepp2k@jabber.org
ICQ: 205544826
Forum
(Forum)
1 December 2008 18:18
6
U know, exercise left to the reader