Mega Code Archive

 
Categories / Ruby / String
 

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