To create an array with empty strings ['a', '', 'b', '', 'c']
(not one space strings ' '
), using %W
I can use %W[a #{} b #{} c]
, also I can concatenate arrays, but is it possible to create array with empty strings using just %w[]
?
-
6no. %w[] has no way to represent zero-length string as an element.– dbenhurCommented Oct 19, 2012 at 22:29
-
See also stackoverflow.com/questions/4064062/… (there the results contain a space - so it is a similar situation, not the same)– knutCommented Oct 23, 2012 at 20:58
-
@knut: I know about escaping space, but that is not what I want– tigCommented Oct 24, 2012 at 14:09
Add a comment
|
3 Answers
A couple of options
%W[a b c #{''} z]
%W[a b c] << " "
(I know this isn't using the %w{}
syntax, but for good measure:
'a,b,c,,z'.split(',')
-
the first option given here is the best IMO. should be marked the correct answer Commented Dec 5, 2014 at 16:28
try using %W instead of %w and use \s escape character for the empty string
%W[a \s b ]
-
1
-
u can use %W[a #{""} b ] or %W[a #{String.new} b ] or %W[a #{} b], but i couldn't find any way you can get a empty string using %w Commented Mar 12, 2014 at 7:13
-
-
use the first suggestion in professormeowingtons's answer above Commented Dec 5, 2014 at 16:28