Tips for creating strings IN Ruby: ruby Summary
% {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.
Copy codeThe Code is 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 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} to start notepad in windows, I will not list the results here (that is a window that everyone is familiar)