[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need to understand code

Junkone

8/11/2006 12:50:00 PM

This is an extract from
http://www.codeforpeople.com/lib/ruby/arrayfields/arrayfields-3....
that i am trying to understand.



relation = pgconn.query sql
relation.size #=> 65536

# yikes! do we really want to re-construct a hash for for each
tuple when
# we already have Arrays?

fields = %w(ssn name position)
table.each{|tuple| tuple.fields = fields}

tuples[34578]['ssn'] #=> 574865032

What does this term mean %w.......

3 Answers

Jano Svitok

8/11/2006 12:55:00 PM

0

On 8/11/06, junkone1@gmail.com <junkone1@gmail.com> wrote:
> fields = %w(ssn name position)

> What does this term mean %w.......

> fields = %w(ssn name position)

is the same as ['ssn', 'name', 'position'], i.e. %w is a syntactic shortcut.
http://www.rubycentral.com/book/langua...

Farrel Lifson

8/11/2006 12:57:00 PM

0

On 11/08/06, junkone1@gmail.com <junkone1@gmail.com> wrote:
> This is an extract from
> http://www.codeforpeople.com/lib/ruby/arrayfields/arrayfields-3....
> that i am trying to understand.
>
>
>
> relation = pgconn.query sql
> relation.size #=> 65536
>
> # yikes! do we really want to re-construct a hash for for each
> tuple when
> # we already have Arrays?
>
> fields = %w(ssn name position)
> table.each{|tuple| tuple.fields = fields}
>
> tuples[34578]['ssn'] #=> 574865032
>
> What does this term mean %w.......
>
>
>

%w(ssn name position) is the same as ["ssn", "name", position"]

Farrel

Daniel Schierbeck

8/11/2006 1:05:00 PM

0

junkone1@gmail.com wrote:
> What does this term mean %w.......

It's a shorthand used for creating arrays of strings:

%w{a b c d} #=> ["a", "b", "c", "d"]

Basically, the character that follows `%w' is the delimiter. So:

%w<delimiter> foo bar baz <delimiter> => ["foo", "bar", "baz"]

i.e.

%w& foo bar & #=> ["foo", "bar"]

If the starting delimiter is `(', `{', `[', or `<', then the matching
closing symbol will be the closing delimiter:

%w<foo bar baz> #=> ["foo", "bar", "baz"]

Read more on <http://www.rubycentral.com/book/languag...


Cheers,
Daniel