Summary of usage of percentage sign (%) in Ruby

Time:2020-3-26

%Q

A string used to replace double quotes. When you need to put many quotes in a string, you can use the following methods directly without adding backslashes (\ “) one by one before the quotes

Copy codeThe code is as follows:
>> %Q(Joe said: “Frank said: “#{what_frank_said}””)
=> “Joe said: “Frank said: “Hello!”””

(…) can also be replaced by other non alphanumeric symbols or paired symbols, such as […],!…!, +… +, {…}, <… >

All of the following are equivalent to the above:

Copy codeThe code is 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, Q writing can be omitted:

Copy codeThe code is 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 codeThe code is 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 represents an array in which elements are enclosed in double quotes

Copy codeThe code is as follows:
>> %W(#{foo} Bar Bar\ with\ space)
=> [“Foo”, “Bar”, “Bar with space”]

%w

Used to represent an array in which elements are enclosed by single quotation marks. Strangely, \ (slash space) will be converted to (space), but other contents will not

Copy codeThe code is as follows:
>> %w(a b c\ d \#e #{1}f)
=> [“a”, “b”, “c d”, “\\#e”, “\#{1}f”]

%x

Use the method to execute a shell script and return standard output

Copy codeThe code is as follows:
>> %x(echo foo:#{foo})
=> “foo:Foo\n”

%r

Syntax approximates% q for regular expressions

Copy codeThe code is as follows:
>> %r(/home/#{foo})
 => “/\\/home\\/Foo/”

%s

Used to represent a symbol, but the expression and other contents will not be converted

Copy codeThe code is as follows:
>> %s(foo)
=> :foo
>> %s(foo bar)
=> :”foo bar”
>> %s(#{foo} bar)
=> :”\#{foo} bar”

%i

Syntax introduced after Ruby 2.0 for generating a symbol array
2.0.0p247 :014 > %i(a b c)
=> [:a, :b, :c]

 

Attachment: another

%{string} is used to create a string in double quotes
%Q {string} is used to create a string in double quotes

%Q!Some String of “Characters”! <==> ” Some String of /”Characters/” “
%Q {string} is used to create a single quoted string
%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 split a string into an array of strings with white space characters and make fewer substitutions
%W {string} is used to split a string into an array of strings with white space characters for more replacement
%W(North South East West) <==> [“North”, “South”, “East”, “West”]
%S {string} is used to generate a symbol object
%X {string} is used to execute the command represented by string
%x{ ls /usr/local } <==> `ls /usr/local`

PS: in the above% notation, {} is used to expand the string. In fact, this {} is just a separator, which can be replaced by other characters, such as (), then the% notation is% (string), and of course, it can also be other characters. For non bracket type separators, the left and right sides should be the same, such as%! String!

Here are a few examples of these representations:

%{string} is used to create a string in double quotes
This representation is exactly the same as% Q {string}. Here is an example of a direct sentence:

Copy codeThe code is as follows:
result = %{hello} 
puts “result is: #{result}, Type is:#{result.class}” 

Result is: Hello, type is: String

%Q {string} is used to create a string in double quotes
%Q {string} is used to create a single quoted string
It can be seen from the description that the difference between the two representations is that one uses double quotation marks and the other uses single quotation marks. Using double quotation marks for strings will make more substitutions for variables in strings, while single quotation marks will make less substitutions. See the example for details. First look at% Q {string}:

Copy codeThe code is as follows:
world = “world” 
result = %Q{hello #{world}} 
puts “result is: #{result}, Type is:#{result.class}”

 
Result is: Hello world, type is: String

Replace with% Q {string}:

Copy codeThe code is as follows:
world = “world” 
result = %q{hello #{world}} 
puts “result is: #{result}, Type is:#{result.class}” 

Result: result is: Hello {world}, type is: String

From the above results, it can be seen that in the case of less substitution, the {world} is parsed into a string instead of calculating the value in this variable.

%R {string} is used to create a regular expression literal
Just as with / reg /, look at the code:

Copy codeThe code is as follows:
result = %r{world} 
puts result =~ “hello world” 
puts “result is: #{result}, Type is:#{result.class}” 

Results: 6 result is: (? – Mix: World), type is: regexp

As you can see, world matches from the 6th character

%W {string} is used to split a string into an array of strings with white space characters and make fewer substitutions
%W {string} is used to split a string into an array of strings with white space characters for more replacement
These two should be the most you’ve seen. By constructing arrays in this way, you can save some commas. Ruby will spoil you, and you won’t need punctuation in the future.
Also give a simple example:

Copy codeThe code is as follows:
result = %w{hello world} 
puts “result is: #{result}, Type is:#{result.class}, length is:#{result.length}” 

Result is: HelloWorld, type is: array, length is: 2

%S {string} is used to generate a symbol object
Direct up code:

Copy codeThe code is 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}” 

Result:
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
For example:
%X {Notepad. Exe} can start Notepad under windows. I will not list the results here (that is a familiar window).

 

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]