Forum: Ruby Explanation on some code

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.
F3afd44c69449d7446f7c920a2ba669d?d=identicon&s=25 Michael Albers (concatenate)
on 2008-12-01 14:39
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.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-12-01 15:00
(Received via mailing list)
On Mon, Dec 1, 2008 at 2:34 PM, Michael Albers
<registraties@concatenate.nl> wrote:
>    'Some more text'
> Thanks a lot.
> --
> Posted via 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 Dober ;)
59c436d6801db18313ff1b27369bc8dd?d=identicon&s=25 Vladimir Fekete (Guest)
on 2008-12-01 15:13
(Received via mailing list)
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
F3afd44c69449d7446f7c920a2ba669d?d=identicon&s=25 Michael Albers (concatenate)
on 2008-12-01 15:22
Thanks for the quick replies. I will figure it out now.

M.
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-12-01 19:12
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-12-01 19:25
(Received via mailing list)
U know, exercise left to the reader ;)
This topic is locked and can not be replied to.