るりまサーチ

最速Rubyリファレンスマニュアル検索!
21件ヒット [1-21件を表示] (0.025秒)

別のキーワード

  1. kernel require
  2. getoptlong require_order
  3. rubygems/custom_require require
  4. irb/ext/use-loader irb_require
  5. require execute

ライブラリ

モジュール

検索結果

Timeout.#timeout(sec, exception_class = nil) {|i| ... } -> object (43.0)

ブロックを sec 秒の期限付きで実行します。 ブロックの実行時間が制限を過ぎたときは例外 Timeout::Error が発生します。

...
require
'timeout'

def calc_pi(min)
loop do
x = rand
y = rand
x**2 + y**2 < 1.0 ? min[0] += 1 : min[1] += 1
end
end

t = 5
min = [ 0, 0]
begin
Timeout.timeout(t){
calc_pi(min)
}
rescue Timeout::Error
puts "timeout"
end

printf
"%d:...
...[1], min[0]*4.0/(min[0]+min[1])
#例
#=> 417519: pi = 3.141443

例 独自の例外を発生させるタイムアウト
#!/usr/bin/env ruby

require
'timeout'

class MYError < Exception;end
begin
Timeout.timeout(5, MYError) {
sleep(30)
}
rescue MYError => err
puts "MY...
...ている) が終了した直後((A)の箇所)で Timeout::Error 例外があがっています。

例 timeout が割り込めない
require
'timeout'
require
'socket'

t = 0.1
start = Time.now
begin
Timeout.timeout(t) {
p TCPSocket.gethostbyname("www.ruby-lang.org")
# (A)...

Timeout.#timeout(sec, exception_class, message) {|i| ... } -> object (43.0)

ブロックを sec 秒の期限付きで実行します。 ブロックの実行時間が制限を過ぎたときは例外 Timeout::Error が発生します。

...
require
'timeout'

def calc_pi(min)
loop do
x = rand
y = rand
x**2 + y**2 < 1.0 ? min[0] += 1 : min[1] += 1
end
end

t = 5
min = [ 0, 0]
begin
Timeout.timeout(t){
calc_pi(min)
}
rescue Timeout::Error
puts "timeout"
end

printf
"%d:...
...[1], min[0]*4.0/(min[0]+min[1])
#例
#=> 417519: pi = 3.141443

例 独自の例外を発生させるタイムアウト
#!/usr/bin/env ruby

require
'timeout'

class MYError < Exception;end
begin
Timeout.timeout(5, MYError) {
sleep(30)
}
rescue MYError => err
puts "MY...
...ている) が終了した直後((A)の箇所)で Timeout::Error 例外があがっています。

例 timeout が割り込めない
require
'timeout'
require
'socket'

t = 0.1
start = Time.now
begin
Timeout.timeout(t) {
p TCPSocket.gethostbyname("www.ruby-lang.org")
# (A)...