Need to understand code

This is an extract from
http://www.codeforpeople.com/lib/ruby/arrayfields/arrayfields-3.5.0/README
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…

On 8/11/06, [email protected] [email protected] 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/language.html#UB

On 11/08/06, [email protected] [email protected] wrote:

tuple when

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

Farrel

[email protected] 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 foo bar baz => [“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”]

Read more on http://www.rubycentral.com/book/language.html

Cheers,
Daniel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs