Ruby Chomp: The Easiest Way to Remove Unwanted New Lines
Ruby is a simple and easy to use programming language. Its ease of use attribute has its roots in the way Ruby has been designed, which in turn shares a lot of similarity with many of the existing programming languages. This makes it convenient for anybody having exposure to any of the existing programming languages to have a start with Ruby right away. The languages that have contributed to the development of Ruby include Perl, Smalltalk, Eiffel, Ada, and Lisp. Amongst these, the latter two are known to be suited more towards programing in the artificial intelligence domain.
Ruby, however, has come to be known more as an open source general purpose programming language that is used more for server side scripting. Coming to specifics, here we walk you through to the use of ‘chomp’ and its use in the Ruby. You can learn more about Ruby programming in this beginners course, or you an read up this step by step tutorial on using Ruby.
What is Chomp?
To begin with, ‘chomp’ is a string method which is built into Ruby. The chomp function helps remove the trailing newline character ie ‘\n’, from any string. Note that it is just one among the dozen odd such string methods that Ruby ships with. It is used with functions such as the ‘gets’ function and is used to remove the ending ‘\n’ character from the string obtained via the gets function. The chomp method comes in handy when displaying output messages and is used in many cases where the ending ‘\n’ character is not desired. Read on to get an understanding of the way the ‘chomp’ method is used in real life programming.
How to use Chomp
Let’s start with a simply program that asks the user for his name. The program then greets the user, followed by a simple message.
puts “What is your name?” name = gets puts “Hi “ + name + “. How is life?”
The program is perhaps the simplest that it can be. It just asks the user for his name and then prints that out followed by the ubiquitous question that we so often ask of ourselves as well as others, “How is life?”
Here the ‘gets’ function prompts the user to enter his name, which needs to be typed when the console asks for it. ‘gets’ will accept a single line of user input and assigns the string to name. The user typically hits the enter key after the name is typed to mark the end of input phase. However, the enter or newline is also recorded by ‘gets’ as a user input, which also gets reflected in the output. In other words, ‘gets’ returns the ‘\n’ character as well together with the input string. You can learn more about using gets function to get user input in this Ruby course.
The output for the above program will be like this:
Hi Dennis. How is life?
There is nothing wrong with the output if that is how you want it to be. However, for cases when the output need to be displayed in a single line. It is here that the ‘chomp’ method comes into play. Let’s modify the above program to ensure the output is reflected in a single line.
puts “What is your name?” name = gets name = name.chomp puts “Hi “ + name + “. How is life?”
Let’s first show what the output will be like.
Hi Dennis. How is life?
The difference here is obvious – both the lines are included on the same line. It is the ‘chomp’ method that brings all the difference as it acts to chop the ‘\n’ character from the end of the input string. The chomp method will only accept the input string from the keyboard while ignoring the enter key pressed at the end of user input. (Chop is another string method in Ruby which can be used to remove the last character of the input string. However, it can’t be used in place of the ‘chomp’ method as the same when used in the above program will lead to the output: “Hi Denni. How is life?”. This makes ‘chomp’ the recommended method for removing trailing newline characters from string input. ) To learn more about using the chomp (or the chop) method, take this practical training course on Ruby.
Here is another program to highlight the functionality of the ‘chomp’ string function.
puts “Let me know your name.” STDOUT.flush name = gets puts ‘Hi ’ + name + ‘. Welcome to Ruby!!!’
The output for the above program will be like this:
Hi Bella. Welcome to Ruby!!!
The same program with ‘chomp’ integrated is like this:
puts “Let me know your name.” STDOUT.flush name = gets.chomp puts ‘Hi ’ + name + ‘. Welcome to Ruby!!!’
The output comes like this (which makes more sense than to have the same spread across two lines)
Hi Bella.Welcome to Ruby!!!
Worth mentioning here, the STDOUT mentioned in the program here is a global constant which represents the standard output stream for the program. Using STDOUT.flush ensures the output appears immediately instead of being held up in a buffered zone for an intermediate period of time. There, the output is held back until enough has gathered to transfer them to the terminal. Using ‘flush’ will flush out any buffered data that may have been accumulated within io to the underlying operating system. This represents Ruby’s internal buffering mechanism and is independent of any buffering that the OS might be incorporating.
Users can also resort to using STDOUT.sync = true which will save the user from making repeated flush calls several times. Also, usage of the flushing mechanism is not mandatory in Ruby but is highly recommended, as you can see in this advanced Ruby course.
Recommended Articles
Top courses in Development
Popular topics
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.