Ruby-isms - %Q, %q, %W, %w, %x, %r, %s

Ruby-isms - %Q, %q, %W, %w, %x, %r, %s

I have been working in a legacy codebase and we have been seeing different styles of ruby syntax. I give my nitpicky thoughts about when to use these and why.

Strings

%Q

This is an alternative for double-quoted strings, when you have more quote characters in a string.Instead of putting backslashes in front of them, you can easily write:

>> %Q(Rick said: "Kevin did you say "I'm a #{funny_name}?"")
=> "Rick said: "Kevin did you say "I'm a rambler?"""

The parenthesis (...) can be replaced with any other non-alphanumeric characters and non-printing characters (pairs), so the following commands are equivalent:

>> %Q(Rick said: "Kevin did you say "I'm a #{funny_name}?"")
>> %Q[Rick said: "Kevin did you say "I'm a #{funny_name}?""]
>> %Q!Rick said: "Kevin did you say "I'm a #{funny_name}?""

I usually avoid using %Q() and prefer HEREDOCs for this scenario. They are much easier to read.

If you do use this, I recommend avoiding using alternate non-alphanumeric characters.

%q

This is the same syntax %Q() but handles single-quoted strings.

>> %q(Matt asks: 'Where is Kevin's Dog's bowl?')
=> "Matt asks: 'Where is Kevin's Dog's bowl?'"

Again, I usually avoid using %Q() and prefer HEREDOCs for this scenario. They are much easier to read.

HEREDOCs - My Preference

I did touch on these before. They are the easiest and don’t require you to consult the docs about the above nuanced differences between %Q() and %q(). With a “Here document” the sytanx involves a starting value <<SOMESTRING and terminating SOMESTRING. The document inside can contain a multiline string with quotes and interpolation ruby values.

>> str = <<-TEXT
  Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr
  persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.
  Cum utinam nonumes nominavi eu, mazim dolor per in.
  #{my_variable}

  Joe said, "How is Kevin's dog?"
TEXT
=>   Lorem ipsum dolor sit amet, duo nusquam minimum id, ius suas elitr
  persius eu. Mel tamquam verterem inciderint in. Solum propriae cum ut.
  Cum utinam nonumes nominavi eu, mazim dolor per in.
  my_variable here

  Joe said, "How is Kevin's dog?"

Arrays

%w

Used for single-quoted array elements.

>> %w(id1 id2 id3)
=> ["id1", "id2", "id3"]

I use this all the time. Mostly because it cuts down on quotes and commas.

%i

Builds single-quoted array elements and converts to symbols.

>> %i(id1 id2 id3)
=> [:id1, :id2, :id3]

I use this all the time too. It is much clearer than typing out a symbol array with commas.

%W

Similar syntax to %Q, this is used to build arrays with double-quoted values.

>>  %W(Cat Dog Dog\ Bites\ Man)
=> ["Cat", "Dog", "Dog Bites Man"]

I strongly discourage using %W! This syntax is very confusing.

%x

This the same the `` method and returns the standard output result of running the command in a shell.

>> %x(echo Hello:#{name})
=> "Hello:world\n"
>>  `echo Hello:#{name}`
=> "Hello:world\n"

Because the `` method is used in other languages, I usually prefer it.

%r

Used for regular expressions.

>> %r(/hello/#{name})
=> "/\\/home\\/Foo/"

The default rubocop rule is to use slashes. I usually stick with that and avoid %r

%s Used for symbols. The big feature is the value is can have spaces and is not subject to expression substitution or escape sequences.

>> %s(symbol)
=> :symbol

>> %s(big symbol)
=> :"big symbol"

>> %s(hellow #{name})
=> :"hellow \#{name}"

This is also of limited value and have never found a good use for this.

Most symbols are better apporach with the default ruby syntax of to_sym:

>> :my_symbol
=> :my_symbol
>> "my_id".to_sym
=> :my_id

In closing, just because something is in a language, doesn’t mean we have to use it. Don’t be afraid to change strange syntax to syntax that is easier for beginners to understand.