Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to %w[text] in Ruby without including \n where it line wraps

Tags:

string

ruby

I want to build an array of a list of state abbreviations, and since it is long, I want to split it over several lines. Using %w or %W, however, includes the \n where the lines break.

STATES = %w(AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME\
  MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD\
  TN TX UT VT VA WA WV WI WY)

So "ME" will be "ME\n", as will "SD" be "SD\n"

How do I solve this problem?

like image 366
George Shaw Avatar asked May 02 '12 03:05

George Shaw


2 Answers

Just drop the slashes and there will not be any new line characters or slashes

like image 121
mikeycgto Avatar answered Nov 04 '22 02:11

mikeycgto


irb(main):003:0* STATES = %w(AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME\
irb(main):004:0]   MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD\
irb(main):005:0]   TN TX UT VT VA WA WV WI WY).collect(&:chomp)
=> ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
like image 26
earlonrails Avatar answered Nov 04 '22 02:11

earlonrails