Percent Literals in Ruby

Working with strings in Ruby is great, and in the beginning of boot-camp at around with your single quotes 'harry' and then you meet "sally".

So harry is great, but he has some limitations, looking at single_quote below things, everything is great until your run into an apostrophe and then it all goes to custard. In order to get things back on the rails you need to start escaping \ the characters that freak out the string formation.

Things that to look up a bit with sally, or do they ? everything is cool beans with the apostrophe’s, but now the quotations marks " are the fly in the ointment.

pet = "rabbit"

single_quote = '\"I really don\'t like quotes\", said Sally\'s cat from it\'s home!'
double_quote = "\"I really don't like quotes\", said Harry's cat from it's home!"

double_quote_interpolation = "\"I really don't like quotes\", said the lonely #{pet} from it's home"
# => "\"I really don't like quotes\", said the rabbit from it's home"

Well both harry and sally will rest easier tonight as percent literals are the way to escape (I liked that one) your string building woes.

We can solve single_quote & double_quote using below:

  %q("I really don't like quotes", said Sally's cat from it's home!)
  # => "\"I really don't like quotes\", said Sally's cat from it's home!"

In order to bring interpellation into the mix we need to capitalise the Q like below:

  %Q("I really don't like quotes", said the lonely #{pet} from it's home)
  # => "\"I really don't like quotes\", said the lonely rabbit from it's home\"

So here are two ways to tidy up your string interpellation, and get rid the need for those pesky escape characters along the way.

Another interesting example using percent literals and strings in arrays is below, though it is open for discussion if it is in fact more readable.

require "rubygems"
require "hpricot"
require "rails"

can be turned into

%w{rubygems hpricot rails}.each { |lib| require lib }


Examples

language = 'Ruby'

%q('Simple' "non-interpolated" String.) # => "'Simple' \"non-interpolated\" String."

%Q(Interpolated "#{language}" String.) # => "Interpolated \"Ruby\" String."

%(Interpolated "#{language}" String (default).) # => "Interpolated \"Ruby\" String (default)."

# Simple non-interpolated String Array:
%w[Ruby Javascript Coffeescript] # => ["Ruby", "Javascript", "Coffeescript"]

# Interpolated String Array:
%W[#{language} Javascript Coffeescript] # => ["Ruby", "Javascript", "Coffeescript"]

#  Simple non-interpolated Symbol Array:
%i[ruby javascript coffeescript] # => [:ruby, :javascript, :coffeescript]

# Interpolated Symbol Array:
%I[#{language.downcase} javascript coffeescript] # => [:ruby, :javascript, :coffeescript]

%x(echo #{language} interpolated shell scripting command)
# => "Ruby interpolated shell scripting command\n"

%r{/#{language} regexp/i} # => /\/Ruby regexp\/i/


Literal Description Bracket type
%q Simple non-interpolated String ()
%Q Interpolated String ()
% Interpolated String (default) ()
%w Simple non-interpolated String Array []
%W Interpolated String Array []
%i Simple non-inteprolated Symbol Array []
%I Interpolated Symbol Array []
%x Interpolated shell command ()
%r Interpolated regular expression {}


References

👉 Next: Eloquent Ruby - Chapters (7..9) 👈 Previous: Http and Http2