Ruby. Why arr =% w {'a', 'b', 'c'} => ["'a',", "'b',", "'c'"]

Based on this indexOf question in Ruby ,

I wonder why in Ruby arr = %w{'a', 'b', 'c'} #=> ["'a',", "'b',", "'c'"]

0
source share
2 answers

%w initializes an array separating the contents in brackets (or other characters) with a space (if you need space to avoid using \ ).

+2
source

%w{one two} is a shortcut to ["one", "two"] : the %w{...} notation accepts space-separated elements as strings for an array.

In fact, this is the whole reason %w{...} : writing arrays without quotes or commas to separate elements, which allows you to use quotes and commas without escaping them.

+2
source

Source: https://habr.com/ru/post/1436541/


All Articles