chop() public

Returns a new String with the last character removed. If the string ends with rn, both characters are removed. Applying chop to an empty string returns an empty string. String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.

"string\r\n".chop #=> "string" "string\n\r".chop #=> "string\n" "string\n".chop #=> "string" "string".chop #=> "strin" "x".chop.chop #=> ""
Show source
Register or log in to add new notes.
April 14, 2011
0 thanks

Example

Chops the last character off a string.

> a = "12345" > a.chop => "1234" > a => "12345" > a.chop! => "1234" > a => "1234