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:

How does chop method work 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 string
puts "string\n".chop # String with \n
puts "string\n\r".chop # String with \n and \r
puts "string".chop.chop # Applying chop twice removes two characters
puts "".chop # Empty string
puts "Hello".chop # Normal string

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved