Space in the ruby array by %w

RubyArrays

Ruby Problem Overview


How can I add space character in the ruby array if I want to use %w(a b c) syntax?

Ruby Solutions


Solution 1 - Ruby

Escape it:

%w(a b\ c) # => ["a", "b c"]

Solution 2 - Ruby

Try this:

>> a = %w(a\  b c)
=> ["a ", "b", "c"]

Solution 3 - Ruby

Try this:

>> a = %W[a \s b]
=> ["a", " ", "b"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVasiliy ErmolovichView Question on Stackoverflow
Solution 1 - RubyGarethView Answer on Stackoverflow
Solution 2 - RubySudhanshuView Answer on Stackoverflow
Solution 3 - Rubynguyentrung206View Answer on Stackoverflow