ruby 小程序_如何告诉Ruby程序等待一段时间?

ruby 小程序

sleep()方法的示例 (Example of sleep() method)

Sometimes, you may need to halt the execution for some time. This may occur when you are working on a geocoding piece or a sidekiq job in a Ruby application. To avoid the Google API query limit, you may need to sleep the program for 1 second. In such case, you will require Ruby sleep() method which is predefined in Ruby's library. You may require to sleep your program for an arbitrary amount of time in many situations.

有时,您可能需要暂停执行一段时间 。 当您在Ruby应用程序中处理地理编码片段sidekiq作业时,可能会发生这种情况。 为避免Google API查询限制,您可能需要将程序Hibernate1秒钟。 在这种情况下,您将需要在Ruby的库中预定义的Ruby sleep()方法 。 在许多情况下,您可能需要在任意时间睡眠程序。

Ruby sleep()方法 (Ruby sleep() method)

sleep() method accepts number of seconds as argument and suspends the thread of execution for the amount of seconds being passed as parameter. The parameter can be passed as number or float or fraction. If you are working with Ruby on Rails, then you get the facility to pass parameter with .seconds, .minutes or .hours and it can be suspended up to .days as well.

sleep()方法接受秒数作为参数,并在执行线程作为参数传递的时间内暂停执行线程。 该参数可以数字,浮点数或小数形式传递。 如果您正在使用Ruby on Rails的工作,那么你得到的设备通过与.seconds,.minutes.hours参数,并可以暂停直至.days为好。

If you do not pass any argument with sleep or do not specify, for how much time you want the execution to be suspended then your program will sleep forever.

如果您不使用sleep传递任何参数或未指定,则您希望执行被挂起多少时间,则程序将永远Hibernate。

Let us understand the concept with the help of short program codes.

让我们借助简短的程序代码来理解概念。

Example 1:

范例1:

=begin
Code to make execution sleep for 1 second.
=end

puts "Hello world! Greetings from Includehelp.com"
sleep 1
puts "bye"

Output

输出量

Hello world! Greetings from Includehelp.com
bye
[Finished in 1.3s]

The above code is written and tested in Sublime Text. There you can check the execution time. The above program is taking .3 seconds in execution and 1 second is the sleep time which is suspending the thread.

上面的代码是用Sublime Text编写和测试的。 在那里您可以检查执行时间。 上面的程序执行需要0.3秒钟,而挂起线程的Hibernate时间是1秒。

Now, let us verify the execution time bypassing another argument in the sleep method.

现在,让我们绕过sleep方法中的另一个参数来验证执行时间。

Example 2:

范例2:

=begin
Code to make execution sleep for 5 seconds.
=end  

puts "Hello world! Greetings from Includehelp.com"
sleep 5
puts "bye"

Output:

输出:

Hello world! Greetings from Includehelp.com
bye
[Finished in 5.3s]

The above code is also written and tested in Sublime Text. The execution time taken by the program is ".3" seconds and the sleep time is 5 seconds because we have passed 5 seconds in the sleep() method as an argument.

上面的代码也是用Sublime Text编写和测试的。 该程序花费的执行时间为“ .3”秒,而睡眠时间为5秒,因为我们已经在sleep()方法中传递了5秒作为参数。

Now let us verify whether sleep method accepts float argument or not with the help of example given below,

现在,让我们借助下面给出的示例来验证sleep方法是否接受float参数,

Example 3:

范例3:

=begin
Code to make execution sleep for 0.75 second.
=end	

puts "Hello world! Greetings from Includehelp.com"
sleep 0.75
puts "bye" 

Output

输出量

Hello world! Greetings from Includehelp.com
bye
[Finished in 1.1s]

With the help of above code, you can understand that sleep variable accepts float argument as well.

借助以上代码,您可以了解sleep变量也接受float参数。

翻译自: https://www.includehelp.com/ruby/how-to-tell-ruby-program-to-wait-for-some-amount-of-time.aspx

ruby 小程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值