Discrimination of Chop and chomp usage in Ruby

Source: Internet
Author: User
Tags chop

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:

Remove the record separator ($ /) from the end of the string, usually \ n. If there is no record separator, nothing is done.
Same as chomp, but str changes and returns.
Remove the last character in str.
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

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.