Custom usage of %w in Ruby

I know that %w or %W is used for constructing an array of strings. But I have a custom requirement:

Using %w, I would like to get the following array pattern:

["First Name", "Middle Name", "Last Name", "Email"]

Is it possible?

The space between the strings are making issues.

Thanks for any help offered :)

-----------------------------------------------------------------------------------

You'll have to use character escaping to escape the space, like so:

result = %w(First\ Name Middle\ Name Last\ Name Email)

Though not sure if that reads any clearer than putting them in quotes.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章