Last active
May 9, 2025 14:59
-
-
Save jakimowicz/df1e4afb6e226e25d678 to your computer and use it in GitHub Desktop.
Revisions
-
jakimowicz revised this gist
Nov 2, 2015 . No changes.There are no files selected for viewing
-
jakimowicz revised this gist
Nov 2, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, 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, available in ruby >= 2.1. >> %I(#{foo} Bar Bar\ with\ space) => [:"\#{foo}", :Bar, :"Bar with space"] -
jakimowicz renamed this gist
Nov 2, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jakimowicz revised this gist
Nov 2, 2015 . 1 changed file with 94 additions and 36 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ### % 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!""" 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 _non-interpolated string_ 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} ' '" ## Arrays ### %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 _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_ 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" ## Regexp ### %r _interpolated regexp_ Used for regular expressions.The syntax is similar to %Q. >> %r(/home/#{foo}) => "/\\/home\\/Foo/" Additionnaly, you can add flags after the closing delimiter, like this >> %r/[a-z]/i => /[a-z]/i -
jakimowicz revised this gist
Nov 2, 2015 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,9 @@ # %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}"") -
jakimowicz renamed this gist
Nov 2, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # %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}"") -
jakimowicz created this gist
Nov 2, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"