Need to understand code

Discussion in 'Ruby' started by junkone1@gmail.com, Aug 11, 2006.

  1. Guest

    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.......
    , Aug 11, 2006
    #1
    1. Advertising

  2. Jan Svitok Guest

    On 8/11/06, <> 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
    Jan Svitok, Aug 11, 2006
    #2
    1. Advertising

  3. On 11/08/06, <> wrote:
    > 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.......
    >
    >
    >


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

    Farrel
    Farrel Lifson, Aug 11, 2006
    #3
  4. 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/language.html>


    Cheers,
    Daniel
    Daniel Schierbeck, Aug 11, 2006
    #4
    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. Ketul Patel
    Replies:
    1
    Views:
    711
    Alvin Bruney
    Nov 29, 2003
  2. Learner
    Replies:
    1
    Views:
    420
    Kevin Spencer
    Jan 5, 2006
  3. nymus
    Replies:
    7
    Views:
    397
    Roedy Green
    Jul 28, 2004
  4. Simon
    Replies:
    7
    Views:
    574
    Simon
    Oct 5, 2005
  5. Jaeho Kim
    Replies:
    0
    Views:
    334
    Jaeho Kim
    Apr 29, 2005
Loading...

Share This Page