What is the chop method in Ruby?
The chop method in Ruby returns a new string with the last character removed. If the last character is a newline \n or a character feed \r, both of them are removed as well. If both these escape sequences come at the end of a string, they are removed together with a single chop command.
The illustration below shows how the chop method works in ruby:
Return value
The chop method returns a new string with the last element removed from the original string.
If chop is applied on an empty string, an empty string is returned.
Example
The code snippet below shows how we can use the chop method in ruby:
puts "string".chop # Normal stringputs "string\n".chop # String with \nputs "string\n\r".chop # String with \n and \rputs "string".chop.chop # Applying chop twice removes two charactersputs "".chop # Empty stringputs "Hello".chop # Normal string
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved