Ruby 输出到与前一个输出相同的行 [英] Ruby outputting to the same line as the previous output

查看:30
本文介绍了Ruby 输出到与前一个输出相同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Ruby 脚本来生成一个 CSV 文件.我的理解是 CSV 文件中的每一行都是表中的一行.

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..

我的脚本通过从包含不同文件中路线位置的原始数据中查找温哥华和卡尔加里来工作.然后脚本转到这些文件以查找实际路线.每次它找到一条路线(例如 Route1)时,脚本都会使用puts"方法输出它.问题是每个输出都在一个新行上.

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.

有没有办法在使用puts"命令时抑制换行命令?

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

推荐答案

是的,使用 print var 代替;puts 自动添加新行,print 不会.

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

这篇关于Ruby 输出到与前一个输出相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆