chomp and chomp! : chomp « String « Ruby






chomp and chomp!


s = "hello\r\n"      # A string with a line terminator
s.chomp!             # => "hello": remove one line terminator from end
s.chomp              # => "hello": no line terminator so no change
s.chomp!             # => nil: return of nil indicates no change made
s.chomp("o")         # => "hell": remove "o" from end
$/ = ";"             # Set global record separator $/ to semicolon
"hello;".chomp       # => "hello": now chomp removes semicolons and end

 








Related examples in the same category

1.The chomp and chop Methods
2.chomp! returns nil without altering the string because there is no record separator at the end of the string