Ruby% w blank

How do I add an empty string as a select option in %w(Mr Ms Mrs Miss)

?

I can do ['', 'Mr', 'Ms', 'Mrs', 'Miss']

, but I would like to use %w

. thank!

+3


source to share


2 answers


Use unshift

, this is an extra object in front:

=> %w(Mr Ms Mrs Miss).unshift ""
#> ["", "Mr", "Ms", "Mrs", "Miss"]

      

Pretty version without parentheses.

Good for your update:



=> %W(#{} Mr Ms Mrs Miss)
#> ["", "Mr", "Ms", "Mrs", "Miss"]

      

explain:

  • %W

    - enable interpolation
  • %W

    - do not allow interpolation
  • #{}

    - empty line
+8


source


Try the following:



%w(Mr Ms Mrs Miss).unshift('')
["", "Mr", "Ms", "Mrs", "Miss"]

      

+1


source







All Articles