Not yet a systematic study of Ruby, recently looking at the Metasploit framework of the exploit will involve the Ruby script, it would be very hard to check the data once again to make some notes.
There are built-in functions for chop and chomp in Ruby strings. The usage of ruby strings chop and chomp I get in http://www.w3cschool.cc/ruby/ruby-string.html is described below:
str.chomp
Remove the record separator ($ /) from the end of the string, usually \ n. If there is no record separator, nothing is done.
str.chomp!
Same as chomp, but str changes and returns.
str.chop
Remove the last character in str.
str.chop!
Same as chop, but str changes and returns.
Single from these words, or some unclear white (seemingly borrowed from the Perl language of the chop and chomp functions of the use), and then Baidu a bit, I http://blog.chinaunix.net/uid-20691105-id-1568659.html get the following:
The difference between chomp and chop:
chomp: remove \ n or \ r at the end of the string
chop: remove the last character at the end of the string, whether it is \ n \ r or ordinary characters
"hello" .chomp # => "hello"
"hello \ n" .chomp # => "hello"
"hello \ r \ n" .chomp # => "hello"
"hello \ n \ r" .chomp # => "hello \ n"
"hello \ r" .chomp # => "hello"
"hello" .chomp ("llo") # => "he"
"string \ r \ n" .chop # => "string"
"string \ n \ r" .chop # => "string \ n"
"string \ n" .chop # => "string"
"string" .chop # => "strin"
I try the string above on this machine one by one, and the output is as follows:
First, you can see that print does not output line breaks (no wrapping), but resolves the escape characters in double quotes, and you can see that print output the newline character in the string \ n and carriage return \ r. I was a little puzzled by the "\ r \ n", "\n\r" The two sequence is not the same, chop and chomp function is how to deal with. From the result of the operation, when the last side of the string is "\ r \ n", "\ r \ n" will be removed, and when the string is followed by "\n\r", only the carriage return will be removed. Well, it's finally clear.
Analysis of Chop and chomp usages in Ruby