0016 Percent_literal

Yes, this piece of code is perfectly correct!

This example uses a percent literal of type String with % as delimiter.

0016-percent_literal_1.png

The list of percent literal is as follows:

  • %, %Q, %q (String)
  • %s (Symbol)
  • %r (Regexp)
  • %x (Backtick => Shell command)
  • %W, %w (String array)
  • %I, %i (Symbol array)

The delimiter is the character of your choice (almost…). The most interesting are the String and Symbol arrays. The difference between lowercase and uppercase is whether or not interpolation is performed.

0016-percent_literal_2.png

Blank characters are not taken into account ("\n" included). To include them, escape them with a "\".

0016-percent_literal_3.png

Small question, does the following code produce an error?

0016-percent_literal_4.png

Link to Ruby documentation for percent literal.