如何使用“看跌期权"?到控制台没有 ruby​​ on rails 的换行符? [英] How can I use "puts" to the console without a line break in ruby on rails?

查看:14
本文介绍了如何使用“看跌期权"?到控制台没有 ruby​​ on rails 的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环的方法——我希望它输出一个."每个循环,所以我可以在控制台中看到它.但是,当我使用 puts ".".

I have a method which goes through a loop -- I want it to output a "." each loop so I can see it in the console. however, it puts a linebreak at the end of each when I use puts ".".

如果有办法让它只有一条连续的线?

If there a way so that it just has a continuous line?

推荐答案

你需要使用 print 而不是 puts.此外,如果您希望点看起来平滑,则需要在每次打印后刷新标准输出缓冲区...

You need to use print instead of puts. Also, if you want the dots to appear smoothly, you need to flush the stdout buffer after each print...

def print_and_flush(str)
  print str
  $stdout.flush
end

100.times do
  print_and_flush "."
  sleep 1
end

我只是在调查 flush 背后的原因来回答 @rubyprince 的评论,并意识到这可以通过简单地使用 $stdout.sync = true...

I was just looking into the reasoning behind flush to answer @rubyprince's comment, and realised this could be cleaned up a little by simply using $stdout.sync = true...

$stdout.sync = true

100.times do
  print "."
  sleep 1
end

这篇关于如何使用“看跌期权"?到控制台没有 ruby​​ on rails 的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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