Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']
]
like image 961
Paul Avatar asked Aug 07 '26 10:08

Paul


1 Answers

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.

like image 132
Winfield Avatar answered Aug 10 '26 00:08

Winfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!