String#chop
str.chop -> new_string Returns:
String · Updated March 13, 2026 · String Methods strings trimming characters copy
The chop method returns a new string with the last character removed. Unlike chop!, this method doesn’t modify the original string—it creates and returns a copy.
Basic Usage
# Remove last character
"hello".chop # => "hell"
"ruby!".chop # => "ruby"
"".chop # => ""
# Original unchanged
str = "hello"
puts str # => "hello"
puts str.chop # => "hell"
puts str # => "hello" (still "hello")
Practical Examples
Path Handling
# Remove trailing slash
path = "/home/user/"
path.chop # => "/home/user"
# Ensure no trailing character
url = "https://example.com/"
url.chop # => "https://example.com"
String Cleanup
# Remove trailing newlines (one character)
"line1\n".chop # => "line1"
"line1\n\n".chop # => "line1\n" (only removes one!)
# For more control, use chomp
"line1\n".chomp # => "line1"
Input Processing
# Clean user input
username = gets.chop
# Unlike chomp, removes ANY trailing character
# Process command arguments
args = "arg1 arg2".chop
CSV Row Processing
# Remove trailing field separator
row = "field1,field2,field3"
row.chop # => "field1,field2,field3"
# If last char is comma
How It Differs from chomp
# chop removes exactly ONE character
"hello\n".chop # => "hello" (removes newline)
"hello\n".chomp # => "hello" (removes newline)
# Different when no newline
"hello".chop # => "hell" (removes 'o')
"hello".chomp # => "hello" (no change, no newline)
Edge Cases
# Empty string
"".chop # => ""
# Single character
"a".chop # => ""
# Multiple newlines
"a\n\n".chop # => "a\n" (one newline remains)
With Regex Alternative
# For more control, use regex
"hello".sub(/.$/, '') # => "hell"
"hello".gsub(/.$/, '') # => "hell" (if one char)
Performance
# chop is fast and efficient for single char removal
# Creates new string (doesn't modify original)
Use chop when you need to remove exactly one character from the end of a string without modifying the original.