Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add comment to line in multiline %w in ruby

I have a multiline string array via percent string like this:

array = %w(test
           foo
           bar)

I want to add a comment message to the foo entry, something like

array = %w(test
           # TODO: Remove this line after fix #1
           foo
           bar)

Is there any way to do it without converting it to basic array like this?

array = ['test',
         # TODO: Remove this line after fix #1
         'foo',
         'bar']
like image 578
ShockwaveNN Avatar asked Sep 23 '16 14:09

ShockwaveNN


2 Answers

I think there is no way to make that work, because %w() evaluates every space delimited element inside it to string.

There's no way from inside the string to make Ruby evaluate that string.

like image 93
Andrey Deineko Avatar answered Nov 03 '22 00:11

Andrey Deineko


The only and tricky way:

array = %W(test
           #@foo
           bar).reject(&:empty?)

Note capital W and reject

like image 25
cema-sp Avatar answered Nov 03 '22 02:11

cema-sp