只是为了好玩,我如何编写一个ruby程序,一次缓慢打印到stdout一个字符?

时间:2022-03-02 00:07:42

I thought this might work

我认为这可行

 "a b c d e f g h i j k".each {|c| putc c ; sleep 0.25}

I expected to see "a b c d e f j" be printed one character at a time with 0.25 seconds between each character. But instead the entire string is printed at once.

我期望看到“a b c d e f j”一次打印一个字符,每个字符之间有0.25秒。但相反,整个字符串立即打印。

5 个解决方案

#1


5  

Two things:

  1. You need to use .each_char to iterate over the characters. In Ruby 1.8, String.each will go line-by-line. In Ruby 1.9, String.each is deprecated.
  2. 您需要使用.each_char来迭代字符。在Ruby 1.8中,String.each将逐行进行。在Ruby 1.9中,不推荐使用String.each。

  3. You should manually flush $stdout if you want the chars to appear immediately. Otherwise, they tend to get buffered so that the characters appear all at once at the end.
  4. 如果希望立即显示字符,则应手动刷新$ stdout。否则,它们往往会被缓冲,以便角色最后一次出现。

.

#!/usr/bin/env ruby
"a b c d d e f g h i j k".each_char {|c| putc c ; sleep 0.25; $stdout.flush }

#2


5  

Two things:

  1. You need to split that string into an array before you use each on it.
  2. 在使用每个字符串之前,需要将该字符串拆分为数组。

  3. Turn off output buffering.

    关闭输出缓冲。

    $stdout.sync = true
    "a b c d d e f g h i j k".split(" ").each {|c| putc c ; sleep 0.25}
    

#3


2  

Ruby buffers output and will flush it to standard output after it reaches a certain size. You can force it to flush like so.

Ruby缓冲输出,并在达到一定大小后将其刷新到标准输出。你可以强迫它像这样冲洗。

"a b c d e f g h i j k".each_char do |char|
   putc char
   $stdout.flush
   sleep 0.25
end

Note: if you don't want spaces between the characters when printed, use .split.each instead of .each_char.

注意:如果在打印时不希望字符之间有空格,请使用.split.each而不是.each_char。

Just for fun: with a definition like this:

只是为了好玩:有这样的定义:

def slowly
  yield.each_char { |c| putc c; $stdout.flush; sleep 0.25 }
end

You would be able to do this:

你可以这样做:

slowly do
  "a b c d e f g h i j k"
end

#4


1  

Try:

%w"a b c d e f g h i j k".each {|c| putc c ; sleep 0.25}

That works as is with Ruby 1.9.2, which doesn't need STDOUT to flush between each write.

这与Ruby 1.9.2一样,在每次写入之间不需要STDOUT来刷新。

If you want the intervening spaces remove %w and use each_char instead of each.

如果您希望插入空格删除%w并使用each_char而不是每个。

#5


0  

This is my way of "slow printing":

这是我的“慢速打印”方式:

for i in "a b c d e f g h i j k".chars.to_a
  print i
  sleep 0.25
end

#1


5  

Two things:

  1. You need to use .each_char to iterate over the characters. In Ruby 1.8, String.each will go line-by-line. In Ruby 1.9, String.each is deprecated.
  2. 您需要使用.each_char来迭代字符。在Ruby 1.8中,String.each将逐行进行。在Ruby 1.9中,不推荐使用String.each。

  3. You should manually flush $stdout if you want the chars to appear immediately. Otherwise, they tend to get buffered so that the characters appear all at once at the end.
  4. 如果希望立即显示字符,则应手动刷新$ stdout。否则,它们往往会被缓冲,以便角色最后一次出现。

.

#!/usr/bin/env ruby
"a b c d d e f g h i j k".each_char {|c| putc c ; sleep 0.25; $stdout.flush }

#2


5  

Two things:

  1. You need to split that string into an array before you use each on it.
  2. 在使用每个字符串之前,需要将该字符串拆分为数组。

  3. Turn off output buffering.

    关闭输出缓冲。

    $stdout.sync = true
    "a b c d d e f g h i j k".split(" ").each {|c| putc c ; sleep 0.25}
    

#3


2  

Ruby buffers output and will flush it to standard output after it reaches a certain size. You can force it to flush like so.

Ruby缓冲输出,并在达到一定大小后将其刷新到标准输出。你可以强迫它像这样冲洗。

"a b c d e f g h i j k".each_char do |char|
   putc char
   $stdout.flush
   sleep 0.25
end

Note: if you don't want spaces between the characters when printed, use .split.each instead of .each_char.

注意:如果在打印时不希望字符之间有空格,请使用.split.each而不是.each_char。

Just for fun: with a definition like this:

只是为了好玩:有这样的定义:

def slowly
  yield.each_char { |c| putc c; $stdout.flush; sleep 0.25 }
end

You would be able to do this:

你可以这样做:

slowly do
  "a b c d e f g h i j k"
end

#4


1  

Try:

%w"a b c d e f g h i j k".each {|c| putc c ; sleep 0.25}

That works as is with Ruby 1.9.2, which doesn't need STDOUT to flush between each write.

这与Ruby 1.9.2一样,在每次写入之间不需要STDOUT来刷新。

If you want the intervening spaces remove %w and use each_char instead of each.

如果您希望插入空格删除%w并使用each_char而不是每个。

#5


0  

This is my way of "slow printing":

这是我的“慢速打印”方式:

for i in "a b c d e f g h i j k".chars.to_a
  print i
  sleep 0.25
end