Open In App

Ruby | String chomp! Method

Last Updated : 13 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

chomp! is a String class method in Ruby which is used to returns new String with the given record separator removed from the end of str (if present). chomp method will also removes carriage return characters (that is it will remove \n, \r, and \r\n) if $/ has not been changed from the default Ruby record separator, t. If $/ is an empty string, it will remove all trailing newlines from the string. It will return nil if no modifications were made.

Syntax: str.chomp!

Parameters: Here, str is the given string.

Returns: A new string having no record separator from the end or nil if no changes were made.

Example 1:




# Ruby program to demonstrate 
# the chomp! method 
       
# Taking a string and 
# using the method 
puts "Ruby".chomp!
puts "Ruby\r\n".chomp 


Output:


Ruby

Example 2:




# Ruby program to demonstrate 
# the chomp! method 
       
# Taking a string and 
# using the method 
puts "String\r\n\r\r\n".chomp!('')
puts "Method".chomp!("tho")


Output:

String



Similar Reads

Ruby | String chomp Method
chomp is a String class method in Ruby which is used to returns new String with the given record separator removed from the end of str (if present). chomp method will also remove carriage return characters (that is it will remove \n, \r, and \r\n) if $/ has not been changed from the default Ruby record separator, t. If $/ is an empty string, it wil
1 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | String inspect Method
inspect is a String class method in Ruby which is used to return a printable version of the given string, surrounded by quote marks, with special characters escaped. Syntax: str.inspect Parameters: Returns: Here, str is the given string. Example 1: # Ruby program to demonstrate # the inspect method # Taking a string and # using the method puts
1 min read
Ruby | String hex Method
hex is a String class method in Ruby which is used to treats the leading characters from the given string as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. Syntax: str.hex Parameters: Here, str is the given string. Returns: A corresponding number. Example 1:
1 min read
Ruby | String include? Method
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str.include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false. Example 1: # Ruby program to demonstrate # the include? method #
1 min read
Ruby | String delete_suffix! Method
delete_suffix! is a String class method in Ruby which is used to return a copy of the given string with trailing suffix deleted or nil if no change was made. Syntax:str.delete_suffix! Parameters: Here, str is the given string. Returns: A copy of the given string with trailing suffix deleted or nil if no change was made. Example 1: C/C++ Code # Ruby
1 min read
Ruby | String dump Method
dump is a String class method in Ruby which is used to generate a version of the given string with all non-printing characters replaced by \nnn notation and all special characters escaped. Syntax: str.dump Parameters: Here, str is the given string. Returns: A new string with all non-printing characters replaced by \nnn notation and all special char
1 min read
Ruby | String each_byte Method
each_byte is a String class method in Ruby which is used to passes each byte in the given string to the given block or returns an enumerator if no block is given. Syntax: str.each_byte {|integer| block } Parameters: Here, str is the given string. Returns: An enumerator. Example 1: # Ruby program to demonstrate # the each_byte method # Taking a stri
1 min read
Ruby | String * Method
String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0. Syntax: str * Integer Parameters: Here, str is the required string and integer is the number of copies. Returns: This method returns the String containing integer copies of the
1 min read
Ruby | String + Method
+() is a String class method in Ruby which is used to return a new string that contains the other specified string concatenated to the given string. Syntax: str + other_str Parameters: Here, str and other_str are the strings which are to be concatenated. Returns: Returns the concatenated string. Example 1: #ruby 2.3.1 # Ruby program to demonstrate
1 min read