%{string} is used to create a string enclosed in double quotes
%q{string} is used to create a string enclosed in double quotes
Copy Code code as follows:
Str=<<end_of_string
A string
End_of_string
%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} '
Results: 6
Result 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 results (it's a familiar window)