【发布时间】:2015-11-19 23:51:57
【问题描述】:
有没有可能达到和这个一样的效果:
"Hello World".each_char do |c|
print c;
sleep(0.05)
end
在终端输出上?我认为必须有一种更简单/更简洁的方法来执行此操作,而不是将上面的代码添加到每个字符串的末尾,然后在每个字符串之后添加新行。
【问题讨论】:
标签: ruby terminal output sleep
有没有可能达到和这个一样的效果:
"Hello World".each_char do |c|
print c;
sleep(0.05)
end
在终端输出上?我认为必须有一种更简单/更简洁的方法来执行此操作,而不是将上面的代码添加到每个字符串的末尾,然后在每个字符串之后添加新行。
【问题讨论】:
标签: ruby terminal output sleep
将其定义为方法:
def slow_print(string)
string.each_char { |c| print c; sleep(0.05) }
end
然后调用它:
slow_print("Hello World")
【讨论】:
puts?
puts 在 Kernel 类上定义。 See this link.