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?
We can also use "\n" ( newline character ) to print a new line whenever we want as used in most of the programming languages.
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.
In puts method, the newline is automatically appended.
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.
Yes, use print var
instead; puts
automatically appends a new line, print
doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With