Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby outputting to the same line as the previous output

Tags:

I am writing a Ruby script to generate a CSV file. My understanding is that each line in a CSV file is a row in a table.

Right now my script generates something looks like this

Vancouver, Calgary, Routes1, Routes2, Routes3, Vancouver, Toronto etc, etc, etc 

but I need it to make it look like this to import it to a DB

Vancouver, Calgary, Routes1, Routes2, Routes3 Vancouver, Toronto, etc etc etc.. 

My script works by looking up Vancouver and Calgary from raw data that contains the locations of the routes in different files. Then the script goes to those files to look for actual routes. Each time it finds a route (eg. Route1), the script outputs it using "puts" method. The problem is that every output is on a new line.

Is there a way to suppress the new line command when using "puts" command?

like image 971
Adam Lee Avatar asked Aug 05 '09 19:08

Adam Lee


People also ask

How do you print on the same line in Ruby?

We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.

What is difference between print and puts in Ruby?

Hi, The difference between print and puts is that puts automatically moves the output cursor to the next line (that is, it adds a newline character to start a new line unless the string already ends with a newline), whereas print continues printing text onto the same line as the previous time.

Does puts add a new line Ruby?

In puts method, the newline is automatically appended.

What does puts stand for in Ruby?

The puts (short for "out*put s*tring") and print commands are both used to display in the console the results of evaluating Ruby code. The primary difference between them is that puts adds a new line after executing, and print does not.


1 Answers

Yes, use print var instead; puts automatically appends a new line, print doesn't.

like image 182
mipadi Avatar answered Oct 22 '22 22:10

mipadi