The chomp()
method, when called on a string, removes the last trailing newline from the string.
chomp(str)
str
: The string we want to remove from any last trailing new line.
This method returns the total number of trailing newlines removed.
# create some strings$greetings = "Hi! Welcome to Ruby!\n";$name = "Ruby Programming Language\n";# use the chomp method$result1 = chomp($greetings);$result2 = chomp($name);$result3 = chomp($greetings, $name);# print resultsprint "$result1 \n";print "$result2 \n";print "$result3";
In the above code snippet:
chomp
method and remove trailing newline characters in the strings we created.