Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Using %W

I have the following which works well:

def steps     %w[hello billing confirmation] end  steps.first 

But I want to do this:

  def step_title     %w['Upload a photo' 'Billing Info' 'Confirmation Screen']   end  steps.first 

How does %w allow for that? I tried a google search but google is weak with these types of characters.

Thanks

like image 951
AnApprentice Avatar asked Dec 15 '10 22:12

AnApprentice


People also ask

What is %W in Ruby on Rails?

%w[hello billing confirmation] is syntax sugar for ["hello", "billing", "confirmation"] . It tells Ruby to break up the input string into words, based on the whitespace, and to return an array of the words.

Where can I use Ruby on Rails?

Ruby on Rails is used in all types of industries to build web apps and services. This includes applications like marketing websites, CMSs, eCommerce sites, and custom web applications. It's a popular web framework for startups because its ease of use makes for quick application development with small teams.

Is Ruby on Rails open source?

Ruby on Rails (sometimes RoR) is the most popular open-source web application framework. It's built with the Ruby programming language. You can use Rails to help you build applications, from simple to complex, there are no limits to what you can accomplish with Rails!


2 Answers

%w creates an "array of words," and uses whitespace to separate each value. Since you want to separate on another value (in this case, whitespace outside sets of quotation marks), just use a standard array:

['Upload a photo', 'Billing Info', 'Confirmation Screen'] 
like image 143
Brian Rose Avatar answered Sep 20 '22 15:09

Brian Rose


%w() is a "word array" - the elements are delimited by spaces.

There are other % things:

%r() is another way to write a regular expression.

%q() is another way to write a single-quoted string (and can be multi-line, which is useful)

%Q() gives a double-quoted string

%x() is a shell command.

like image 30
Constantine Avatar answered Sep 22 '22 15:09

Constantine