Usage of percent signs (%) in Ruby

Source: Internet
Author: User

% Q

Used to replace double quotation marks. When you need to put a lot of quotation marks in the string, you can directly use the following method without adding a backslash (\ ") One by one before the quotation marks (\")
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 or pair symbols, such as [...],!...!, +... +, {...}, <...>, Etc.

All of the following statements 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 quotation mark 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 is used to represent an array in which elements are enclosed by double quotation marks.
Copy codeThe Code is as follows:
>>% W (# {foo} Bar \ with \ space)
=> ["Foo", "Bar", "Bar with space"]

% W

It is used to indicate the array in which elements are enclosed by single quotes. It is strange that \ (slash space) will be converted to (Space), but other content 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 the standard output content.
Copy codeThe Code is as follows:
>>% X (echo foo :#{ foo })
=> "Foo: Foo \ n"

% R

The syntax is similar to % Q for regular expressions.
Copy codeThe Code is as follows:
>>% R (/home/# {foo })
=> "/\/Home \/Foo /"

% S

It is used to represent the symbol, but does not convert the expressions and other content.
Copy codeThe Code is as follows:
>>% S (foo)
=>: Foo
>>% S (foo bar)
=>: "Foo bar"
>>% S (# {foo} bar)
= >: "\ # {Foo} bar"

% I

The syntax introduced after Ruby 2.0 is used to generate a symbol array.
2.0.0p247: 014> % I (a B c)
=> [: A,: B,: c]

 


Appendix: another article


% {String} is used to create a String enclosed by double quotation marks.
% Q {String} is used to create a String enclosed by double quotation marks.

% Q! Some String of "Characters "! <=> "Some String of/" Characters/""
% Q {String} is used to create a String enclosed by single quotes.
% Q! Some String of "Characters "! <=> 'Some String of Characters'
% R {String} is used to create a regular expression literal value.
% R {/usr/bin/} <=> // usr // bin ///
% W {String} is used to split a String into a String array with blank characters for less replacement.
% W {String} is used to split a String into a String array with white space characters for replacement.
% 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: In the above several % notation, {} is used to expand the String. In fact, this {} is only a separator and can be replaced with other characters, such (), % notation is % (String). Of course, it can also be another character. For non-bracket separators, the left and right sides must be the same, for example, %! String!

Below are some examples of these representations:

% {String} is used to create a String enclosed by double quotation marks.
This representation is exactly the same as % Q {String}. Here is an example of the result:
Copy codeThe Code is as follows:
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 enclosed by double quotation marks.
% Q {String} is used to create a String enclosed by single quotes.
From the description, we can see that the difference between the two representations is that one uses double quotation marks and the other uses single quotation marks. Strings with double quotation marks will replace more variables in the strings, while those with single quotation marks will be replaced less. 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: result is: hello world, Type is: String

Change to % 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

As shown in the preceding results, # {world} is parsed into a string without calculating the value in the variable.

% R {String} is used to create a regular expression literal value.
Just like using the/reg/method, check the Code:
Copy codeThe Code is as follows: result = % r {world}
Puts result = ~ "Hello world"
Puts "result is: # {result}, Type is: # {result. class }"
Result: 6 result is :(? -Mix: world), Type is: Regexp

We can see that world matches from 6th characters.

% W {String} is used to split a String into a String array with blank characters for less replacement.
% W {String} is used to split a String into a String array with white space characters for replacement.
These two should have seen the most. Using this method to construct an array can save some commas. Ruby will spoil everyone, and we will not need punctuation in the future.
A simple example is also given:
Copy codeThe Code is as follows: result = % w {hello world}
Puts "result is: # {result}, Type is: # {result. class}, length is: # {result. length }"
Result: result is: helloworld, Type is: Array, length is: 2

% S {String} is used to generate a symbolic object.
Directly add the code first:
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

It can be seen that the symbol objects generated in the two methods are exactly the same.

% X {String} is used to execute the command represented by String.
For example:
Using xforwardednotepad.exe} can start notepad in windows. Here I will not list the results (that is a window that everyone is familiar ).

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.