Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space in the ruby array by %w

Tags:

arrays

ruby

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

like image 626
Vasiliy Ermolovich Avatar asked Oct 31 '10 16:10

Vasiliy Ermolovich


People also ask

What does %w do in Ruby?

%w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.

How do you check whitespace in Ruby?

If you are using Rails, you can simply use: x. blank? This is safe to call when x is nil, and returns true if x is nil or all whitespace.

How do you check if an array contains a value Ruby?

To check if a value is in the array, you can use the built-in include? method. The include? method returns true if the specified value is in the array and false if not.

How do you split an array in Ruby?

split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches.


1 Answers

Escape it:

%w(a b\ c) # => ["a", "b c"] 
like image 118
Gareth Avatar answered Sep 29 '22 07:09

Gareth