別のキーワード
ライブラリ
- base64 (12)
- benchmark (24)
- bigdecimal (24)
-
bigdecimal
/ math (72) - erb (60)
- json (24)
- objspace (24)
- open-uri (16)
- open3 (72)
- shellwords (24)
- syslog (48)
- timeout (21)
モジュール
- Base64 (12)
- Benchmark (24)
- BigMath (96)
-
ERB
:: DefMethod (12) -
ERB
:: Util (48) - JSON (24)
- Kernel (16)
- ObjectSpace (24)
- Open3 (72)
- Shellwords (24)
- Syslog (48)
- Timeout (21)
キーワード
- E (12)
- PI (12)
-
allocation
_ sourcefile (12) -
allocation
_ sourceline (12) - atan (12)
- cos (12)
- decode64 (12)
-
def
_ erb _ method (12) - exp (12)
- h (12)
-
html
_ escape (12) - log (12)
- measure (12)
- open (40)
- open! (12)
-
pipeline
_ rw (24) -
pipeline
_ w (24) - popen3 (24)
-
pretty
_ generate (12) -
pretty
_ unparse (12) - realtime (12)
- reopen (12)
- shellescape (12)
- shelljoin (12)
- sin (12)
- sqrt (12)
- timeout (21)
- u (12)
-
url
_ encode (12)
検索結果
先頭5件
-
Timeout
. # timeout(sec , exception _ class = nil) {|i| . . . } -> object (55.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......例外を発生させるタイムアウト
#!/usr/bin/env ruby
require 'timeout'
class MYError < Exception;end
begin
Timeout.timeout(5, MYError) {
sleep(30)
}
rescue MYError => err
puts "MYError"
puts err
end
=== 注意
timeout による割り込みは Thread......ている) が終了した直後((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 (55.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......例外を発生させるタイムアウト
#!/usr/bin/env ruby
require 'timeout'
class MYError < Exception;end
begin
Timeout.timeout(5, MYError) {
sleep(30)
}
rescue MYError => err
puts "MYError"
puts err
end
=== 注意
timeout による割り込みは Thread......ている) が終了した直後((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)... -
Open3
. # popen3(*cmd) -> [IO , IO , IO , Thread] (31.0) -
外部プログラム cmd を実行し、そのプロセスの標準入力、標準出力、標準エラー 出力に接続されたパイプと実行したプロセスを待つためのスレッドを 4 要素の 配列で返します。
...準エラー
出力に接続されたパイプと実行したプロセスを待つためのスレッドを 4 要素の
配列で返します。
require 'open3'
stdin, stdout, stderr, wait_thr = *Open3.popen3("/usr/bin/nroff -man")
@param cmd 実行するコマンドを指定します。
@retu......lose します。この場合はブロックの最後の式の結果を返します。
require 'open3'
Open3.popen3("read stdin; echo stdout; echo stderr >&2") {|stdin, stdout, stderr, wait_thr|
stdin.puts "stdin"
stdin.close # または close_write
p stdout.read
p stderr.r......
require 'open3'
Open3.popen3({"foo" => "1", "bar" => "2"}, "env") {|i, o, e, t|
i.close
print o.read
}
#=> ...
foo=1
bar=2
Kernel.#spawnと同様に、引数リストの最後にオプションをハッシュ形式
で指定する事ができます。
例:
require... -
Open3
. # popen3(*cmd) {|stdin , stdout , stderr , wait _ thr| . . . } -> () (31.0) -
外部プログラム cmd を実行し、そのプロセスの標準入力、標準出力、標準エラー 出力に接続されたパイプと実行したプロセスを待つためのスレッドを 4 要素の 配列で返します。
...準エラー
出力に接続されたパイプと実行したプロセスを待つためのスレッドを 4 要素の
配列で返します。
require 'open3'
stdin, stdout, stderr, wait_thr = *Open3.popen3("/usr/bin/nroff -man")
@param cmd 実行するコマンドを指定します。
@retu......lose します。この場合はブロックの最後の式の結果を返します。
require 'open3'
Open3.popen3("read stdin; echo stdout; echo stderr >&2") {|stdin, stdout, stderr, wait_thr|
stdin.puts "stdin"
stdin.close # または close_write
p stdout.read
p stderr.r......
require 'open3'
Open3.popen3({"foo" => "1", "bar" => "2"}, "env") {|i, o, e, t|
i.close
print o.read
}
#=> ...
foo=1
bar=2
Kernel.#spawnと同様に、引数リストの最後にオプションをハッシュ形式
で指定する事ができます。
例:
require... -
BigMath
. # E(prec) -> BigDecimal (25.0) -
自然対数の底 e を prec で指定した精度で計算します。
...で計算します。
@param prec 計算結果の精度。
@raise ArgumentError prec に 0 以下が指定された場合に発生します。
//emlist[][ruby]{
require "bigdecimal/math"
puts BigMath::E(2) #=> 0.27e1
puts BigMath::E(4) #=> 0.2718e1
puts BigMath::E(10) #=> 0.2718281828e1
//}... -
BigMath
. # PI(prec) -> BigDecimal (25.0) -
円周率を prec で指定した精度で計算します。
...が指定された場合に発生します。
//emlist[][ruby]{
require "bigdecimal/math"
puts BigMath::PI(2) #=> 0.31415926535897932384671233672993238432e1
puts BigMath::PI(5) #=> 0.31415926535897932384627534923029509162e1
puts BigMath::PI(10) #=> 0.3141592653589793238462643388813853786957412e1... -
JSON
. # pretty _ generate(object , options = nil) -> String (25.0) -
Ruby のオブジェクトを JSON 形式の文字列に変換して返します。
...照してください。
//emlist[例][ruby]{
require "json"
hash = { "name": "tanaka", "age": 19 }
puts JSON.generate(hash)
# => {"name":"tanaka","age":19}
puts JSON.pretty_generate(hash)
# => {
# "name": "tanaka",
# "age": 19
# }
puts JSON.pretty_generate(hash, space: "\t")
# => {... -
JSON
. # pretty _ unparse(object , options = nil) -> String (25.0) -
Ruby のオブジェクトを JSON 形式の文字列に変換して返します。
...照してください。
//emlist[例][ruby]{
require "json"
hash = { "name": "tanaka", "age": 19 }
puts JSON.generate(hash)
# => {"name":"tanaka","age":19}
puts JSON.pretty_generate(hash)
# => {
# "name": "tanaka",
# "age": 19
# }
puts JSON.pretty_generate(hash, space: "\t")
# => {... -
Open3
. # pipeline _ rw(*cmds) -> [IO , IO , [Thread]] (25.0) -
指定したコマンドのリストをパイプで繋いで順番に実行します。最初の コマンドの標準入力に書き込む事も最後のコマンドの標準出力を受けとる事も できます。
...待つため
のスレッドの配列を配列で返します。
例:
require "open3"
Open3.pipeline_rw("sort", "cat -n") {|stdin, stdout, wait_thrs|
stdin.puts "foo"
stdin.puts "bar"
stdin.puts "baz"
# sortコマンドにEOFを送る。
stdin.close...