To get the desired result of ["0", "[0,", "1]", "{foo:", "0}"]
instead of ["0,", "[0,", "1],", "{foo:", "0}"]
when using %w
in Ruby, you can modify the input to remove the unwanted commas.
Here's how you can achieve it:
main.rb133 chars6 lines
Explanation:
gsub
method with a regular expression /[\[\]{},:]/
to replace all occurrences of brackets [ ]
, braces { }
, commas ,
, and colons :
in the input string with a space ' '
.split
method to split the modified string into an array of individual words.output
variable and printed using p
.Note: %w
is a shorthand notation for creating an array of words in Ruby, where elements are separated by whitespace. By default, it treats commas as part of the word itself, resulting in the unwanted commas in your original output.
gistlibby LogSnag