#Summary of usage of some percent sign (%) in Ruby #This article mainly introduces the usage summary of some percent sign (%) in Ruby, and the friend you need can refer to the followingWhat_frank_said ="hello!"
#%Q #the string used to replace the double quotation marks. When you need to put a lot of quotes in the string, you can use the following method instead of adding a backslash (\ ") to the quotation marks.Puts%Q (1.Joe said:"Frank said:"#{What_frank_said} "") # "#" Cannot save=Begin execution Results="1.Joe said:"Frank said:"hello!"""=End#(...) Other non-alphanumeric symbols or paired symbols can also be substituted, such as [...],!...!, +...+,{...}, <...> etc. #The following wording is all equivalent to the above:Puts%q!2.joe said:"Frank said:"#{what_frank_said} ""!Puts%q[3.joe said:"Frank said:"#{What_frank_said} ""]Puts%q+4.joe said:"Frank said:"#{What_frank_said} "" +#Besides, Q writing can also be omitted:Puts%/5.joe said:"Frank said:"#{What_frank_said} ""/=Begin execution Results="5.Joe said:"Frank said:"hello!"""=End#%q#similar to%q, but represents a single-quote stringPuts%q (6.Joe said:'Frank said: "#{what_frank_said}"')=Begin execution Results="6.Joe said: ' Frank said:"#{what_frank_said} "'"=End#%W#The syntax is approximate to%q, which represents an array in which the elements are enclosed in double quotation marks.Foo ="Foo"arr=%W (#{foo} Bar bar\ with\ space)Puts arr#= = ["Foo", "Bar", "Bar with space"]# %w #used to represent an array where the elements are enclosed in quotation marks. It is strange that \ (slash space) will be converted to (a space), but the rest of the content will not.Arrs =%w (a b c\ d \#e #{1}f g\h)Puts Arrs#= = ["A", "B", "C D", "\ #e", "#{1}f", "g\h"]#%x#use the ' method to execute a shell script and return the standard output content.Puts%x (echo foo:#{foo}) #=> "foo:foo\n"#%r#The syntax is approximate to%q and is used for regular expressions.Puts%r (/home/#{foo}) #=> "/\/home\/foo/"#%s#used to represent the symbol, but it does not convert content such as an expressionPuts%s (foo)#=: FooPuts%s (foo bar)#+ : "foo bar"Puts%s (#{foo} bar) #=>: "\#{foo} bar"#%i#syntax introduced after Ruby 2.0, used to generate a symbol arraySarr =%i (a b C)#= = [: A,: B,: c]puts Sarr
#总结:# %{string} is used to create a string with double quotation marks.# %q{string} is used to create a string with double quotation marks.Puts%q! Some String of "characters"!#= = "Some String of/" characters/""# %q{string} is used to create a string that uses single quotes.Puts%q! Some String of "characters"!#= = "Some String of" characters ""# %r{string} is used to create a regular expression literalPuts%r{/usr/bin/}#=/\/usr\/bin\/# %w{string} is used to divide a string into a string array with a blank word segmentation for less substitution# %w{string} is used to divide a string into a string array with a blank word segmentation, making more substitutionsPuts%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 the String#puts%x{ls/usr/local} #=> ' ls/usr/local ' Linux run#PS: The above several% notation with {} to extend the String, in fact, this {} is only a separator, can be replaced by other characters, such as (), then% notation is% (String), of course, it can be other characters, for the non-bracket type of the delimiter, left and right side to the same, such as%! string!#Let me give you a few examples of these representations:# %{string} is used to create a string with double quotation marks.#This notation is exactly the same as %q{string} , and here is a direct example of the result:result =%{hello}puts"result is: #{result}, Type Is:#{result.class}"#Result: Result Is:hello, Type is:string# %q{string} is used to create a string with double quotation marks.# %q{string} is used to create a string that uses single quotes.#it can be seen from the description that the difference between the two representations is one using double quotes and one using single quotation marks. A string using double quotes will replace more variables in the string, while single quotes are less likely to be replaced, see example. First look at %q{string}:World =" World"result=%q{hello#{World}}Puts"result is: #{result}, Type Is:#{result.class}"#Result: Result Is:hello world, Type is:string#change to %q{string}:World =" World"result=%q{hello#{World}}Puts"result is: #{result}, Type Is:#{result.class}"#Result: Result Is:hello #{world}, Type is:string#As can be seen from the above results, #{world} is parsed into a string instead of the value in the variable, with less substitution. # %r{string} is used to create a regular expression literal#just like using the/reg/method, look at the code:result =%r{world}puts Result=~"Hello--world"puts"result is: #{result}, Type Is:#{result.class}"#Results: 8 result is: (?-mix:world), Type is:regexp#as you can see, World starts with a 8th character match# %w{string} is used to divide a string into a string array with a blank word segmentation for less substitution# %w{string} is used to divide a string into a string array with a blank word segmentation, making more substitutions#These two should be the most people have seen, in this way to construct an array, you can save some commas, Ruby is really spoiled everyone, after all do not use punctuation. #also give a simple example:result =%W{hello world}puts"result is: #{result}, Type Is:#{result.class}, length is:#{result.length}"#results: Result was: ["Hello", "World"], Type is:array, length is:2# %s{string} is used to generate a symbol object#directly on the first code:result =%S{hello world}puts"result is: #{result}, Type Is:#{result.class}"sym= :"Hello World"puts"The symbol is the same: #{sym = = result}"#results: #result Is:hello World, Type is:symbol #the the Same:true#as you can see, the symbol objects generated in these two modes are exactly the same# %x{string} is used to execute the command represented by the String, such as:%x{notepad.exe}#You can start Notepad under Windows, and I'm not going to list the results (that's a familiar window).
[No000010] Summary of usage of some percent sign (%) in Ruby