%q
A string that replaces double quotes. When you need to put a lot of quotes in a string, you can use the following method directly without adding a backslash (\) to the quotation mark.
Copy Code code as follows:
>>%q (Joe said: "Frank said:" #{what_frank_said} ")
=> "Joe said:" Frank said: "Hello!" ""
(...) Other non-alphanumeric symbols or pairs of symbols can also be substituted, such as [...],!...!, +...+,{...}, <...>.
The following wording is all equivalent to the above:
Copy Code code as follows:
>>%q! Joe said: "Frank said:" #{what_frank_said} ""!
>>%q[joe said: "Frank said:" #{what_frank_said} ""]
>>%q+joe said: "Frank said:" #{what_frank_said} "" +
In addition, we can omit Q writing:
Copy Code code as follows:
>>%/joe said: "Frank said:" #{what_frank_said} ""
=> "Joe said:" Frank said: "Hello!" ""
%q
Similar to%q, but represents a single quote string
Copy Code code as follows:
>>%q (Joe said: ' Frank said: ' #{what_frank_said} ')
=> "Joe said: ' Frank said: ' \#{what_frank_said} '"
%w
The syntax is similar to%Q, which is used to represent an array in which elements are enclosed in double quotes.
Copy Code code as follows:
>>%w (#{foo} Bar bar\ with\ space)
=> ["Foo", "Bar", "Bar with Spaces"]
%w
An array that represents the elements in which the element is enclosed in quotation marks. The strange thing is that \ (slash space) will be converted to (spaces), but other content will not.
Copy Code code as follows:
>>%w (a b c\ d \ #e #{1}f)
=> ["A", "B", "C D", "\ #e", "\#{1}f"]
%x
Use the ' method to execute a section of the shell script and return the standard output content.
Copy Code code as follows:
>>%x (Echo Foo:#{foo})
=> "foo:foo\n"
%r
Syntax is approximate to%q and is used for regular expressions.
Copy Code code as follows:
>>%r (/home/#{foo})
=> "/\\/home\\/foo/"
%s
Used to represent symbol, but does not translate content such as expressions
Copy Code code as follows:
>>%s (foo)
=>: Foo
>>%s (foo bar)
=>: "Foo bar"
>>%s (#{foo} bar)
=>: "\#{foo} Bar"
%i
Syntax, introduced after Ruby 2.0, to generate an array of symbol
2.0.0p247:014 >%i (a b C)
=> [: A,: B,: c]
Attached: Another article
%{string} is used to create a string enclosed in double quotes
%q{string} is used to create a string enclosed in double quotes
%q! Some String of "Characters"! <==> "Some String of/" characters/""
%q{string} is used to create a string that is enclosed in single quotes
%q! Some String of "Characters"! <==> ' Some String of Characters '
%r{string} is used to create a regular expression literal
%r{/usr/bin/} <==>///usr//bin///
%w{string} is used to divide a string into an array of strings in a blank segmentation, with fewer substitutions
%w{string} is used to divide a string into an array of strings in a white-space segmentation, with more substitutions
%w (North South East West) <==> ["North", "South", "East", "West"]
%s{string} is used to generate a symbolic object
%x{string} is used to execute the command represented by String
%x{ls/usr/local} <==> ' ls/usr/local '
PS: The above% notation uses {} to extend the String, in fact, this {} is only a kind of separator, can be replaced by other characters, such as (), then the% notation is% (String), of course, can also be other characters, for non-bracket type of separator, the left and right sides to the same, such as%! string!
Let me give you a few examples of these representations:
%{string} is used to create a string enclosed in double quotes
This notation is exactly the same as%q{string}, and here's a direct example to see the result:
Copy Code code as follows:
result =%{hello}
Puts ' result is: #{result}, Type Is:#{result.class} '
Results: Result Is:hello, Type is:string
%q{string} is used to create a string enclosed in double quotes
%q{string} is used to create a string that is enclosed in single quotes
You can see from the description that the difference between the two representations is one that uses double quotes, and one that uses single quotes. strings that use double quotes are more likely to replace the variables in the string, while the single quotes do fewer substitutions, see examples. First look at the%q{string}:
Copy Code code as follows:
World = ' World '
result =%q{hello #{world}}
Puts ' result is: #{result}, Type Is:#{result.class} '
Result: Results Is:hello world, Type is:string
Replace with%q{string}:
Copy Code code as follows:
World = ' World '
result =%q{hello #{world}}
Puts ' result is: #{result}, Type Is:#{result.class} '
Results: Result Is:hello #{world}, Type is:string
As you can see from the results above, #{world} is parsed into a string rather than the value of the variable in the case of less substitution.
%r{string} is used to create a regular expression literal
Just like using/reg/, look at the code:
Copy Code code as follows:
result =%r{world}
Puts result =~ "Hello World"
Puts ' result is: #{result}, Type Is:#{result.class} '
Result: 6 results is: (?-mix:world), Type is:regexp
As you can see, World begins with the 6th character.
%w{string} is used to divide a string into an array of strings in a blank segmentation, with fewer substitutions
%w{string} is used to divide a string into an array of strings in a white-space segmentation, with more substitutions
These two should be the most people have seen, in this way to construct an array, you can save a few commas, Ruby is really spoiled everyone, and later we do not use punctuation.
Also give a simple example:
Copy Code code as follows:
result =%w{hello World}
Puts ' result is: #{result}, Type Is:#{result.class}, length is:#{result.length} '
Results: Result Is:helloworld, Type is:array, length Is:2
%s{string} is used to generate a symbolic object
Direct first Code:
Copy Code code as follows:
result =%s{hello World}
Puts ' result is: #{result}, Type Is:#{result.class} '
Sym =: "Hello World"
Puts "The two symbol is the same: #{sym = = result}"
Results:
Result Is:hello World, Type Is:symbol
The two symbol is the Same:true
As you can see, the symbol objects generated in these two ways are exactly the same
%x{string} is used to execute the command represented by String
Like what:
%x{notepad.exe} can start Notepad under Windows, where I don't list the results (it's a familiar window).