Need to understand code

This is an extract from
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

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

tuple when

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


[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”]


%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”]



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