Paul
Paul

Reputation: 26650

Ruby: multidimensional %w array?

Could a multidimensional array be defined with a single %w directive?

For example:

the_array = %w(one two three{some separator}four five{some separator}six seven eight nine)

I need this to build an array with variable amount of items in a row:

the_array = [
    ['one', 'two', 'three'],
    ['four', 'five'],
    ['six', 'seven', 'eight', 'nine']
]

Upvotes: 3

Views: 922

Answers (1)

Winfield
Winfield

Reputation: 19145

You can do the inverse, use the short-hand word array notation in a standard array brackets.

the_array = [%w(one two three), %w(four five), %w(six seven eight nine)]

The goal of the short-hand word notation is to save all the punctuation in terms of commas and quotation marks.

Upvotes: 4

Related Questions