るりまサーチ

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

別のキーワード

  1. _builtin b
  2. string b
  3. b
  4. b string
  5. b _builtin

クラス

キーワード

検索結果

<< 1 2 > >>

Thread#fetch(name, default = nil) {|name| ... } -> object (18251.0)

name に関連づけられたスレッドに固有のデータを返します。 name に対応するスレッド固有データがない時には、引数 default が 与えられていればその値を、ブロックが与えられていれば そのブロックを評価した値を返します。

...与えられていれば
そのブロックを評価した値を返します。

@param name スレッド固有データのキーを文字列か Symbol で指定します。
@param default name に対応するスレッド固有データがない時の返り値を指定します。
@raise KeyError...
...と発生します。

//emlist[例][ruby]{
th = Thread.new { Thread.current[:name] = 'A' }
th.join
th.fetch(:name) # => "A"
th.fetch(:fetch, 'B') # => "B"
th.fetch('name') {|name| "Thread" + name} # => "A"
th.fetch('fetch') {|name| "Thread" + name} # => "Threadfetch"
//}

@see Thread#[]...

Hash#fetch(key) -> object (18247.0)

key に関連づけられた値を返します。該当するキーが登録されてい ない時には、引数 default が与えられていればその値を、ブロッ クが与えられていればそのブロックを評価した値を返します。

...default が与えられていればその値を、ブロッ
クが与えられていればそのブロックを評価した値を返します。

fetch
はハッシュ自身にデフォルト値が設定されていても単に無視します(挙動に変化がありません)。

@param key 探...
.../emlist[例][ruby]{
h = {one: nil}
p h[:one],h[:two] #=> nil,nil これではキーが存在するのか判別できない。
p h.fetch(:one) #=> nil
p h.fetch(:two) # エラー key not found (KeyError)
p h.fetch(:two,"error")...
...#=> "error"
p h.fetch(:two){|key|"#{key} not exist"} #=> "two not exist"
p h.fetch(:two, "error"){|key| #=> "two not exist"
"#{key} not exist" # warning: block supersedes default value argument
} # 警告が表...

Hash#fetch(key) {|key| ... } -> object (18247.0)

key に関連づけられた値を返します。該当するキーが登録されてい ない時には、引数 default が与えられていればその値を、ブロッ クが与えられていればそのブロックを評価した値を返します。

...default が与えられていればその値を、ブロッ
クが与えられていればそのブロックを評価した値を返します。

fetch
はハッシュ自身にデフォルト値が設定されていても単に無視します(挙動に変化がありません)。

@param key 探...
.../emlist[例][ruby]{
h = {one: nil}
p h[:one],h[:two] #=> nil,nil これではキーが存在するのか判別できない。
p h.fetch(:one) #=> nil
p h.fetch(:two) # エラー key not found (KeyError)
p h.fetch(:two,"error")...
...#=> "error"
p h.fetch(:two){|key|"#{key} not exist"} #=> "two not exist"
p h.fetch(:two, "error"){|key| #=> "two not exist"
"#{key} not exist" # warning: block supersedes default value argument
} # 警告が表...

Hash#fetch(key, default) -> object (18247.0)

key に関連づけられた値を返します。該当するキーが登録されてい ない時には、引数 default が与えられていればその値を、ブロッ クが与えられていればそのブロックを評価した値を返します。

...default が与えられていればその値を、ブロッ
クが与えられていればそのブロックを評価した値を返します。

fetch
はハッシュ自身にデフォルト値が設定されていても単に無視します(挙動に変化がありません)。

@param key 探...
.../emlist[例][ruby]{
h = {one: nil}
p h[:one],h[:two] #=> nil,nil これではキーが存在するのか判別できない。
p h.fetch(:one) #=> nil
p h.fetch(:two) # エラー key not found (KeyError)
p h.fetch(:two,"error")...
...#=> "error"
p h.fetch(:two){|key|"#{key} not exist"} #=> "two not exist"
p h.fetch(:two, "error"){|key| #=> "two not exist"
"#{key} not exist" # warning: block supersedes default value argument
} # 警告が表...

Array#fetch(nth) -> object (18223.0)

nth 番目の要素を返します。

...も存在しなかった場合に発生します。

//emlist[例][ruby]{
a = [1, 2, 3, 4, 5]
b
egin
p a.fetch(10)
rescue IndexError => err
puts err #=> index 10 out of array
end

p a.fetch(10, 999) #=> 999

result = a.fetch(10){|nth|
print "#{nth} はありません。\n"
999
}
p result #=>...

絞り込み条件を変える

Array#fetch(nth) {|nth| ... } -> object (18223.0)

nth 番目の要素を返します。

...も存在しなかった場合に発生します。

//emlist[例][ruby]{
a = [1, 2, 3, 4, 5]
b
egin
p a.fetch(10)
rescue IndexError => err
puts err #=> index 10 out of array
end

p a.fetch(10, 999) #=> 999

result = a.fetch(10){|nth|
print "#{nth} はありません。\n"
999
}
p result #=>...

Array#fetch(nth, ifnone) -> object (18223.0)

nth 番目の要素を返します。

...も存在しなかった場合に発生します。

//emlist[例][ruby]{
a = [1, 2, 3, 4, 5]
b
egin
p a.fetch(10)
rescue IndexError => err
puts err #=> index 10 out of array
end

p a.fetch(10, 999) #=> 999

result = a.fetch(10){|nth|
print "#{nth} はありません。\n"
999
}
p result #=>...

Hash#fetch_values(key, ...) -> [object] (6228.0)

引数で指定されたキーに関連づけられた値の配列を返します。

...emlist[例][ruby]{
h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }

h.fetch_values("cow", "cat") # => ["bovine", "feline"]
h.fetch_values("cow", "bird") # raises KeyError
h.fetch_values("cow", "bird") { |k| k.upcase } # => ["bovine", "BIRD"]
//}

@s...
...ee Hash#values_at, Hash#fetch...

Hash#fetch_values(key, ...) { |key| ... } -> [object] (6228.0)

引数で指定されたキーに関連づけられた値の配列を返します。

...emlist[例][ruby]{
h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }

h.fetch_values("cow", "cat") # => ["bovine", "feline"]
h.fetch_values("cow", "bird") # raises KeyError
h.fetch_values("cow", "bird") { |k| k.upcase } # => ["bovine", "BIRD"]
//}

@s...
...ee Hash#values_at, Hash#fetch...

Data.define(*args) {|subclass| block } -> Class (120.0)

Data クラスに新しいサブクラスを作って、それを返します。

...ッドが定義されています。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred", 5)
p fred.name # => "Fred"
p fred.age # => 5
//}

メンバの値を書き換えることはできません。

//emlist[例][ruby]{
Dog = Data.define(:name, :age)
fred = Dog.new("Fred",...
...ruby]{
class HTTPFetcher
Response = Data.define(:body)
NotFound = Data.define

def get(url)
# ダミーの実装
if url == "http://example.com/"
Response.new(body: "Current time is #{Time.now}")
else
NotFound.new
end
end
end

def fetch(url)
fetch
er = HTTPFetcher...
....new
case fetcher.get(url)
in HTTPFetcher::Response(body)
b
ody
in HTTPFetcher::NotFound
:NotFound
end
end

p fetch("http://example.com/") # => "Current time is 2023-01-10 10:00:53 +0900"
p fetch("http://example.com/404") # => :NotFound
//}

@param args 値オブジェクトの...

絞り込み条件を変える

Thread#[](name) -> object | nil (120.0)

name に対応したスレッドに固有のデータを取り出します。 name に対応するスレッド固有データがなければ nil を返し ます。

...ます。

@param name スレッド固有データのキーを文字列か Symbol で指定します。

//emlist[例][ruby]{
[
Thread.new { Thread.current["name"] = "A" },
Thread.new { Thread.current[:name] = "B" },
Thread.new { Thread.current["name"] = "C" }
].each do |th|
th.join
puts...
...hread:0x00000002a541a8 dead>: B
# => #<Thread:0x00000002a54130 dead>: C
//}

Thread#[] と Thread#[]= を用いたスレッド固有の変数は
Fiber を切り替えると異なる変数を返す事に注意してください。

//emlist[][ruby]{
def meth(newvalue)
b
egin
oldvalue = Thread.c...
...数に与えるブロックがFiberを切り替える場合は動的スコープとしては
正しく動作しません。

//emlist[][ruby]{
f = Fiber.new {
meth(1) {
Fiber.yield
}
}
meth(2) {
f.resume
}
f.resume
p Thread.current[:name]
# => nil if fiber-local
# => 2 if thread-local (The...
<< 1 2 > >>