简体   繁体   English

如何使用Ruby在将每个字符打印成字符串之前进行延迟

[英]How to make delay before each character is printed in a string using Ruby

Does anyone know how to create a small delay (perhaps 0.1 seconds) between the printing of each character in a string in Ruby? 有谁知道如何在Ruby中在字符串中的每个字符打印之间创建一个小的延迟(可能为0.1秒)?

I have tried some other methods (shown below); 我尝试了其他方法(如下所示); however, I'm certain that a simpler method exists as the method im using is time (and space) consuming: 但是,我确定存在一个更简单的方法,因为im使用的方法很耗时间(和空间):

#ugly/boring method vvv

print "h"
sleep 0.1
print "e"
sleep 0.1
print "l"
sleep 0.1
print "l"
sleep 0.1
print "o"

I have done some research on the topic and found a method under C and php but no one seems to have an answer for Ruby. 我已经对该主题进行了一些研究,并发现了在C和php下的一种方法,但是似乎没人能找到Ruby的答案。

Thanks in advance for the help. 先谢谢您的帮助。

"hello".each_char do |c|
  sleep 0.1
  print c
end

Docs: String#each_char 文件: String#each_char

You could also monkey-patch the string class: 您还可以猴子修补字符串类:

class String
  def print_slowly
    self.each_char do |c|
      sleep 0.1
      print c
    end
  end
end

#usage
"hello".print_slowly

A super simple approach would be something like this: 一个超级简单的方法是这样的:

"hello".split(//).each do |c|
  sleep 0.1 
  print c
end

Here's a more verbose explanation: 这是更详细的解释:

string = "hello"
split_that_string = string.split(//);

for character in split_that_string
   sleep 0.1
   print character
end

You could also make a method for it: 您也可以为此创建一个方法:

def print_a_string_one_character_at_a_time(string)
  array_of_characters = string.split(//);
  for character in array_of_characters
    sleep 0.1
    print character
  end
end 

and then you can pass any string to that method. 然后您可以将任何字符串传递给该方法。 Here's an example: 这是一个例子:

print_a_string_one_character_at_a_time("Hello, World!")

That will type out Hello, World! 这将输入Hello,World! one character at a time (and return the array of characters). 一次一个字符(并返回字符数组)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby在每个字符处将字符串分成7个字符块 - Ruby split string into 7 character chunks at each character 如何在 ruby 的字符串中的每个字符之间添加字符? - How do I add characters between each character in a string in ruby? 如何使用Ruby替换文本文件中每个字符的高位? - How to replace high bit for each character in a text file using Ruby? 如何从字符串的开头到在Ruby中的字符串中的特定索引之前的字符的最后一次出现取子字符串 - How to take a substring from the beginning of a string up to the last occurrence of a character that is before a specific index in the string in Ruby Ruby - 从字符串中删除 1 或 2 个字符,使其成为回文 - Ruby - Remove 1 or 2 character from a string to make it a palindrome 如何将输入字符串转换为包含该字符串的每个字符的数组-ruby - How to convert an input string into an array which contains each character of that string - ruby 如何在 Ruby 中使用 .gsub 替换字符串的每 4 个字符? - How to replace every 4th character of a string using .gsub in Ruby? 将字符串的每个字符显示为下划线,每个下划线之间都有一个空格 - display each character of a string as an underscore with a space between each underscore ruby 迭代 ruby​​ 1.8.6 (each_char) 中字符串的每个字符 - iterating over each character of a String in ruby 1.8.6 (each_char) 如何在Ruby中以递增顺序将后缀添加到打印的字符串? - How to add a postfix in incremental order to a printed string in Ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM