Kernel#chomp
str.chomp([separator]) -> string chomp and chop are String methods that remove trailing characters. The key difference: chomp removes the record separator (typically newline), while chop unconditionally removes the last character.
Syntax
string.chomp(separator=$/)
string.chop
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
separator | String | $/ (input record separator) | The string to remove from the end. Typically \n for newlines. |
Examples
Basic usage with chomp
The chomp method is most commonly used when reading lines from files or standard input. Each line typically ends with a newline character, and chomp removes exactly that character without disturbing the rest of the content.
text = "hello world\n"
text.chomp
# => "hello world"
# Without newline, chomp returns a copy
text = "hello world"
text.chomp
# => "hello world"
This is the simplest chomp case because the newline is the only trailing character that should go away. When the separator is present, the method trims it and leaves the rest of the line intact. The default separator is $/, Ruby’s input record separator, which is normally "\n" but can be changed globally.
Using chomp with custom separator
# Remove trailing slashes
path = "/usr/local/bin/"
path.chomp("/")
# => "/usr/local/bin"
# Remove multiple characters
data = "record1|record2|"
data.chomp("|")
# => "record1|record2"
Custom separators make the method useful for formats that use a specific terminator instead of a newline. The rule stays narrow, so the code can remove the ending token without touching the rest of the record. This is helpful when you control the serialization format and know exactly what the trailing marker looks like.
Using chop
text = "hello"
text.chop
# => "hell"
# chop always removes one character
text = "hello\n"
text.chop
# => "hello"
chop is the broader option because it always removes one character, even when that character is not a newline. That makes it handy for quick cleanup, but also easier to misuse if the trailing character still matters. Unlike chomp, which only acts when the separator matches, chop always modifies the string — a useful guarantee in some situations but a risk in others.
Multiple consecutive removals
text = "hello world\n\n"
text.chomp
# => "hello world\n"
# Use chomp! (mutating version) for multiple passes
text = "hello world\n\n"
text.chomp!
text.chomp!
# => "hello world"
Multiple passes are sometimes necessary when the data has more than one trailing separator. The mutating version shows that the method can be repeated, but it still stays focused on the end of the string rather than on all whitespace. When you need to remove multiple trailing separators in a row, chaining chomp! calls is the idiomatic Ruby approach.
Common Patterns
Reading lines from a file
# Each line read by gets includes newline; chomp removes it
File.readlines("file.txt").each do |line|
process(line.chomp)
end
# Or more concisely with chomp:
File.each_line("file.txt") { |line| process(line.chomp) }
Line-based input is where chomp feels most natural, because the method removes the transport noise without changing the line content itself. That keeps the processing step predictable and easy to read. Each line that gets returns comes with a trailing newline, and chomp strips exactly that character before the rest of the program sees the data.
Removing trailing whitespace
# chomp only removes the separator, not all whitespace
text = "hello world \n"
text.chomp
# => "hello world "
# For all trailing whitespace, use strip:
text.strip
# => "hello world"
This contrast is important because strip solves a broader problem. If you only want to remove the record separator and keep other spacing, chomp stays the safer choice. The narrower the cleanup, the less likely it is to accidentally strip meaningful whitespace that was intentionally placed.
Building command strings
# Common pattern: build command, ensure no trailing separator
commands = ["git add .", "git commit -m 'fix'", "git push"]
command = commands.join(" && ").chomp(" && ")
# => "git add . && git commit -m 'fix' && git push"
That pattern is useful when you assemble text from pieces and need to clean off exactly one known ending. The separator argument keeps the rule explicit, which makes the final string easier to reason about. This approach works for any fixed delimiter — paths, record terminators, or custom markers — as long as the ending is predictable.
Keep newline handling specific
chomp is the right tool when you want to remove record separators without stripping away other trailing whitespace. That makes it a natural fit for file input, line-based protocols, and user input that may or may not end with a newline. The method reads like a contract: remove the separator that belongs to the record, then leave the rest alone. That distinction is useful because a string often carries meaningful spacing that you do not want to erase by accident.
In practice, that means chomp is usually the first cleanup step after reading a line or receiving a frame from another system. The method stays narrow, which makes it easier to trust in code that should not mutate more than necessary.
Use the separator when the format is known
Passing a custom separator makes chomp more flexible than many people expect. It can trim a trailing slash from a path, remove a pipe character from a record, or handle any other fixed end marker that belongs to the data format. Because the method only touches the end of the string, it is easy to predict. You can use it confidently when the format is stable and the trailing token has a clear meaning.
That predictability is why chomp works so well in line-oriented code. The separator is part of the rule, so the method keeps the logic local instead of spreading cleanup across several steps.
Prefer the least destructive option
The general rule is simple: choose chomp when you know exactly what should disappear, and reach for broader cleanup methods only when the whole string needs normalizing. That choice keeps your code easier to reason about because it avoids removing characters that were supposed to stay. In practice, this means chomp often appears right after reading a line or receiving a protocol frame, where the goal is to remove transport noise while preserving the content itself.
That distinction is useful because the safer method is usually the more specific one. If only the record separator should go away, chomp keeps the rest of the string intact and makes the intent obvious.
chomp vs chop
| Method | Removes | Use case |
|---|---|---|
chomp | Record separator (usually \n) | Processing file input, user input |
chop | Last character unconditionally | Removing trailing newlines regardless of OS |