Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Explanation on some code

Reply
Thread Tools

Explanation on some code

 
 
Michael Albers
Guest
Posts: n/a
 
      12-01-2008
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/.

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      12-01-2008
On Mon, Dec 1, 2008 at 2:34 PM, Michael Albers
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Vladimir Fekete
Guest
Posts: n/a
 
      12-01-2008
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/.
>
>


 
Reply With Quote
 
Michael Albers
Guest
Posts: n/a
 
      12-01-2008
Thanks for the quick replies. I will figure it out now.

M.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      12-01-2008
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      12-01-2008
U know, exercise left to the reader

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I noticed something with rand and I'm looking for some explanation. mulligan.kyle@gmail.com C Programming 9 03-10-2013 09:54 AM
Could you give me some explanation on this Perl line fl Perl Misc 7 11-03-2010 11:38 PM
looking for explanation of bind2nd() source code responsible C++ 4 08-05-2008 03:51 PM
JCP code- can anybody gve explanation uthra Java 1 11-10-2006 09:04 PM
Need some explanation rahul8143@gmail.com C Programming 70 07-21-2005 11:09 PM



Advertisments