Explanation on some code

Discussion in 'Ruby' started by Michael Albers, Dec 1, 2008.

  1. 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/.
     
    Michael Albers, Dec 1, 2008
    #1
    1. Advertising

  2. Michael Albers

    Robert Dober Guest

    On Mon, Dec 1, 2008 at 2:34 PM, 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 =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.
    > --
    > 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 ::=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 ;)
     
    Robert Dober, Dec 1, 2008
    #2
    1. Advertising

  3. 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/.
    >
    >
     
    Vladimir Fekete, Dec 1, 2008
    #3
  4. Michael Albers, Dec 1, 2008
    #4
  5. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Dec 1, 2008
    #5
  6. Michael Albers

    Robert Dober Guest

    U know, exercise left to the reader ;)
     
    Robert Dober, Dec 1, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. uthra
    Replies:
    1
    Views:
    406
    John Gordon
    Nov 10, 2006
  2. Need some explanation

    , Jul 8, 2005, in forum: C Programming
    Replies:
    70
    Views:
    1,338
    Mark McIntyre
    Jul 22, 2005
  3. responsible
    Replies:
    4
    Views:
    340
    red floyd
    Aug 5, 2008
  4. fl
    Replies:
    7
    Views:
    182
  5. Replies:
    9
    Views:
    222
    Keith Thompson
    Mar 10, 2013
Loading...

Share This Page