Skip to content

Instantly share code, notes, and snippets.

@jakimowicz
Last active May 9, 2025 14:59
Show Gist options
  • Select an option

  • Save jakimowicz/df1e4afb6e226e25d678 to your computer and use it in GitHub Desktop.

Select an option

Save jakimowicz/df1e4afb6e226e25d678 to your computer and use it in GitHub Desktop.

Revisions

  1. jakimowicz revised this gist Nov 2, 2015. No changes.
  2. jakimowicz revised this gist Nov 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions the % notation in ruby.md
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ Used for single-quoted array elements. The syntax is similar to %Q, but single-q

    _interpolated array of symbols, separated by whitespace_

    Generates an array of symbols, similar to %W.
    Generates an array of symbols, similar to %W, available in ruby >= 2.1.

    >> %I(#{foo} Bar Bar\ with\ space)
    => => [:foo, :Bar, :"Bar with space"]
    @@ -69,7 +69,7 @@ Generates an array of symbols, similar to %W.

    _non-interpolated array of symbols, separated by whitespace_

    Generates an array of symbols, similar to %W.
    Generates an array of symbols, similar to %W, available in ruby >= 2.1.

    >> %I(#{foo} Bar Bar\ with\ space)
    => [:"\#{foo}", :Bar, :"Bar with space"]
  3. jakimowicz renamed this gist Nov 2, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. jakimowicz revised this gist Nov 2, 2015. 1 changed file with 94 additions and 36 deletions.
    130 changes: 94 additions & 36 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,61 +1,119 @@
    # %Q, %q, %W, %w, %x, %r, %s, %i

    Perl-inspired notation to quote strings: by using % (percent character) and specifying a delimiting character.

    Any single non-alpha-numeric character can be used as the delimiter, ```%[including these]```, ```%?or these?```, ```%~or even these things~```.

    ## Strings

    ### %Q
    ### % or %Q

    _interpolated string_

    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(Joe said: "Frank said: "#{what_frank_said}"")
    => "Joe said: "Frank said: "Hello!"""
    >> %Q(Joe said: "Frank said: "#{what_frank_said}"")
    => "Joe said: "Frank said: "Hello!"""

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

    >> %Q!Joe said: "Frank said: "#{what_frank_said}""!
    >> %Q[Joe said: "Frank said: "#{what_frank_said}""]
    >> %Q+Joe said: "Frank said: "#{what_frank_said}""+
    >> %Q!Joe said: "Frank said: "#{what_frank_said}""!
    >> %Q[Joe said: "Frank said: "#{what_frank_said}""]
    >> %Q+Joe said: "Frank said: "#{what_frank_said}""+

    You can use also:

    >> %/Joe said: "Frank said: "#{what_frank_said}""/
    => "Joe said: "Frank said: "Hello!"""
    >> %/Joe said: "Frank said: "#{what_frank_said}""/
    => "Joe said: "Frank said: "Hello!"""

    ### %q

    _non-interpolated string_

    %q
    Used for single-quoted strings.The syntax is similar to %Q, but single-quoted strings are not subject to expression substitution or escape sequences.

    >> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
    => "Joe said: 'Frank said: '\#{what_frank_said} ' '"
    >> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
    => "Joe said: 'Frank said: '\#{what_frank_said} ' '"

    ## Arrays

    %W
    ### %W

    _interpolated array of words, separated by whitespace_

    Used for double-quoted array elements.The syntax is similar to %Q

    >> %W(#{foo} Bar Bar\ with\ space)
    => ["Foo", "Bar", "Bar with space"]
    >> %W(#{foo} Bar Bar\ with\ space)
    => ["Foo", "Bar", "Bar with space"]

    %w
    Used for single-quoted array elements.The syntax is similar to %Q, but single-quoted elements are not subject to expression substitution or escape sequences.
    ### %w

    _non-interpolated array of words, separated by whitespace_

    Used for single-quoted array elements. The syntax is similar to %Q, but single-quoted elements are not subject to expression substitution or escape sequences.

    >> %w(#{foo} Bar Bar\ with\ space)
    => ["\#{foo}", "Bar", "Bar with space"]

    ### %I

    _interpolated array of symbols, separated by whitespace_

    Generates an array of symbols, similar to %W.

    >> %I(#{foo} Bar Bar\ with\ space)
    => => [:foo, :Bar, :"Bar with space"]


    ### %i

    _non-interpolated array of symbols, separated by whitespace_

    Generates an array of symbols, similar to %W.

    >> %I(#{foo} Bar Bar\ with\ space)
    => [:"\#{foo}", :Bar, :"Bar with space"]

    ## Symbols

    ### %s

    _interpolated string_

    Used for symbols. It’s not subject to expression substitution or escape sequences.

    >> %s(foo)
    => :foo

    >> %s(foo bar)
    => :"foo bar"

    >> %s(#{foo} bar)
    => :"\#{foo} bar"

    ## Shell command

    ### %x

    _interpolated shell command_

    >> %w(#{foo} Bar Bar\ with\ space)
    => ["\#{foo}", "Bar", "Bar with space"]
    %x
    Uses the ` method and returns the standard output of running the command in a subshell.The syntax is similar to %Q.

    >> %x(echo foo:#{foo})
    => "foo:Foo\n"
    %r
    Used for regular expressions.The syntax is similar to %Q.
    >> %x(echo foo:#{foo})
    => "foo:Foo\n"

    >> %r(/home/#{foo})
    => "/\\/home\\/Foo/"
    %s
    Used for symbols.It’s not subject to expression substitution or escape sequences.
    ## Regexp

    ### %r

    _interpolated regexp_

    Used for regular expressions.The syntax is similar to %Q.

    >> %s(foo)
    => :foo
    >> %r(/home/#{foo})
    => "/\\/home\\/Foo/"

    >> %s(foo bar)
    => :"foo bar"
    Additionnaly, you can add flags after the closing delimiter, like this

    >> %s(#{foo} bar)
    => :"\#{foo} bar"
    >> %r/[a-z]/i
    => /[a-z]/i
  5. jakimowicz revised this gist Nov 2, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    # %Q
    # %Q, %q, %W, %w, %x, %r, %s, %i

    ## 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(Joe said: "Frank said: "#{what_frank_said}"")
  6. jakimowicz renamed this gist Nov 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt → gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    %Q
    # %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(Joe said: "Frank said: "#{what_frank_said}"")
  7. jakimowicz created this gist Nov 2, 2015.
    56 changes: 56 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    %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(Joe said: "Frank said: "#{what_frank_said}"")
    => "Joe said: "Frank said: "Hello!"""
    The parenthesis “(…)” can be replaced with any other non-alphanumeric characters and non-printing characters (pairs), so the following commands are equivalent:

    >> %Q!Joe said: "Frank said: "#{what_frank_said}""!
    >> %Q[Joe said: "Frank said: "#{what_frank_said}""]
    >> %Q+Joe said: "Frank said: "#{what_frank_said}""+
    You can use also:

    >> %/Joe said: "Frank said: "#{what_frank_said}""/
    => "Joe said: "Frank said: "Hello!"""

    %q
    Used for single-quoted strings.The syntax is similar to %Q, but single-quoted strings are not subject to expression substitution or escape sequences.

    >> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
    => "Joe said: 'Frank said: '\#{what_frank_said} ' '"

    %W
    Used for double-quoted array elements.The syntax is similar to %Q

    >> %W(#{foo} Bar Bar\ with\ space)
    => ["Foo", "Bar", "Bar with space"]

    %w
    Used for single-quoted array elements.The syntax is similar to %Q, but single-quoted elements are not subject to expression substitution or escape sequences.

    >> %w(#{foo} Bar Bar\ with\ space)
    => ["\#{foo}", "Bar", "Bar with space"]

    %x
    Uses the ` method and returns the standard output of running the command in a subshell.The syntax is similar to %Q.

    >> %x(echo foo:#{foo})
    => "foo:Foo\n"

    %r
    Used for regular expressions.The syntax is similar to %Q.

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

    %s
    Used for symbols.It’s not subject to expression substitution or escape sequences.

    >> %s(foo)
    => :foo

    >> %s(foo bar)
    => :"foo bar"

    >> %s(#{foo} bar)
    => :"\#{foo} bar"