るりまサーチ

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

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

ライブラリ

クラス

キーワード

検索結果

<< 1 2 > >>

KeyError#key -> object (29120.0)

KeyError の原因となったメソッド呼び出しのキーを返します。

...
Key
Error の原因となったメソッド呼び出しのキーを返します。

@raise ArgumentError キーが設定されていない時に発生します。

例:

h = Hash.new
begin
h.fetch('gumby'*20)
rescue KeyError => e
p e.message # => "key not found: \"gumbygumby...
...gumbygumbygumbygumbygumbygumbygumbygumbygumbygumbyg..."
p 'gumby'*20 == e.key # => true
end...

Data#deconstruct_keys(array_of_names_or_nil) -> hash (14113.0)

self のメンバの名前と値の組を Hash で返します。

...返します。

//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)

distance = Measure.new(10, 'km')
distance.deconstruct_keys(nil) # => {:amount=>10, :unit=>"km"}
distance.deconstruct_keys([:amount]) # => {:amount=>10}
//}

このメソッドは以下のようにパターンマッチ...
...で利用されます。

//emlist[例][ruby]{
Measure = Data.define(:amount, :unit)
distance = Measure.new(10, 'km')

case distance
in amount:, unit: 'km' # 裏側で #deconstruct_keys を呼ぶ
puts "It is #{amount} kilometers away"
else
puts "Don't know how to handle it"
end
# "It is 10 kilom...

KeyError#receiver -> object (11013.0)

KeyError の原因となったメソッド呼び出しのレシーバを返します。

...
Key
Error の原因となったメソッド呼び出しのレシーバを返します。

@raise ArgumentError レシーバが設定されていない時に発生します。

例:

h = Hash.new
begin
h.fetch('gumby'*20)
rescue KeyError => e
p e.message # => "key not found:...

Hash#[](key) -> object | nil (8154.0)

key に関連づけられた値を返します。

...key に関連づけられた値を返します。

該当するキーが登録されていない時には、デフォルト値を返します。

デフォルト値と値としての nil を区別する必要が
ある場合は Hash#fetch または Hash#key? を使ってください。

@param key...
..."all"}
p h[:ab] #=> "some"
p h[:ef] #=> nil

h1 = Hash.new("default value")
p h1[:non] #=> "default value"

h2 = Hash.new {|*arg| arg}
p h2[:non] #=> [{}, :non]
//}

@see Hash.new, Hash#fetch,Hash#values_at,Hash#key?, Hash#default, Hash#default_proc...

ObjectSpace::WeakMap#[]=(key, value) (8148.0)

引数 key から引数 value への参照を作成します。

...引数 key から引数 value への参照を作成します。

@param key 参照元のオブジェクトを指定します。

@param value 参照先のオブジェクトを指定します。

//emlist[例][ruby]{
weak_map = ObjectSpace::WeakMap.new
key
= "text"
weak_map[key] = "test" # => test
w...
...eak_map[key] # => test
//}...

絞り込み条件を変える

Hash#default(key) -> object | nil (8144.0)

ハッシュのデフォルト値を返します。

... key をブロックに渡して評価し、その結果を返します。

@param key デフォルトのブロックにキーとして渡されます。

//emlist[例][ruby]{
h = Hash.new("default")
p h.default #=> "default"
p h.default(:some) #=> "default"
p h #=>{}

h = Hash.new{|hash, key...
...| hash[key] ="default" }
p h.default #=> nil
p h.default(:some) #=> "default"
p h #=> {:some=>"default"}

h = Hash.new
p h.default #=> nil
p h.default(:some) #=> nil
p h #=> {}
//}

@see Hash#default=, Hash#default_proc...

Thread#thread_variable_get(key) -> object | nil (8130.0)

引数 key で指定した名前のスレッドローカル変数を返します。

...引数 key で指定した名前のスレッドローカル変数を返します。

[注意]: Thread#[] でセットしたローカル変数(Fiber ローカル変数)と
異なり、Fiber を切り替えても同じ変数を返す事に注意してください。

例:

Thread.new {
Thread....
...nt.thread_variable_set("foo", "bar") # スレッドローカル
Thread.current["foo"] = "bar" # Fiber ローカル

Fiber.new {
Fiber.yield [
Thread.current.thread_variable_get("foo"), # スレッドローカル
Thread.current["foo"],...

Hash#merge(*others) {|key, self_val, other_val| ... } -> Hash (8126.0)

selfとothersのハッシュの内容を順番にマージ(統合)した結果を返します。 デフォルト値はselfの設定のままです。

...300}
h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
h1.merge(h2) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>46, "c"=>300}
h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
h1...
...=> 'B', 3 => 'C', 4 => 'D'}

p foo.merge(bar)
# => {1=>"a", 2=>"B", 3=>"C", 4=>"D"}
p foo # => {1=>"a", 2=>"b", 3=>"c"}

p foo.merge!(bar) {|key, foo_val, bar_val| foo_val + bar_val }
# => {1=>"a", 2=>"bB", 3=>"cC", 4=>"D"}
p foo # => {1=>"a", 2=>"bB", 3=>"cC", 4=>"D"}
//}

//emlist[...
...{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#replace...

Struct#dig(key, ...) -> object | nil (8126.0)

self 以下のネストしたオブジェクトを dig メソッドで再帰的に参照して返し ます。途中のオブジェクトが nil であった場合は nil を返します。

...す。途中のオブジェクトが nil であった場合は nil を返します。

@param key キーを任意個指定します。

//emlist[例][ruby]{
klass = Struct.new(:a)
o = klass.new(klass.new({b: [1, 2, 3]}))

o.dig(:a, :a, :b, 0) # => 1
o.dig(:b, 0) # =...

Thread#thread_variable_set(key, value) (8124.0)

引数 key で指定した名前のスレッドローカル変数に引数 value をセットしま す。

...引数 key で指定した名前のスレッドローカル変数に引数 value をセットしま
す。

[注意]: Thread#[] でセットしたローカル変数(Fiber ローカル変数)と
異なり、セットした変数は Fiber を切り替えても共通で使える事に注意してく...
...ださい。

//emlist[例][ruby]{
thr = Thread.new do
Thread.current.thread_variable_set(:cat, 'meow')
Thread.current.thread_variable_set("dog", 'woof')
end
thr.join # => #<Thread:0x401b3f10 dead>
thr.thread_variables # => [:dog, :cat]
//}

@see Thread#thread_variable_get, Threa...

絞り込み条件を変える

Hash#merge(other) {|key, self_val, other_val| ... } -> Hash (8120.0)

selfとotherのハッシュの内容をマージ(統合)した結果を返します。デフォルト値はselfの設定のままです。

...h2 = { "b" => 254, "c" => 300 }
h1.merge() # => {"a"=>100, "b"=>200}
h1.merge(h2) # => {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
# => {"a"=>100, "b"=>54, "c"=>300}
h1 # => {"a"=>100, "b"=>200}
//}

//emlist[][ruby]{
foo = {1...
...=> 'B', 3 => 'C', 4 => 'D'}

p foo.merge(bar)
# => {1=>"a", 2=>"B", 3=>"C", 4=>"D"}
p foo # => {1=>"a", 2=>"b", 3=>"c"}

p foo.merge!(bar) {|key, foo_val, bar_val| foo_val + bar_val }
# => {1=>"a", 2=>"bB", 3=>"cC", 4=>"D"}
p foo # => {1=>"a", 2=>"bB", 3=>"cC", 4=>"D"}
//}

//emlist[...
...{:Australia => 'Sydney',
:France => 'Paris'
}
end
end

h = {:Germany => 'Berlin',
:Australia => 'Canberra',
:France => 'Paris'
}

# 暗黙の変換
p h.merge(Foo.new) # => {:Germany=>"Berlin", :Australia=>"Sydney", :France=>"Paris"}
//}

@see Hash#update,Hash#replace...

Hash#to_h {|key, value| block } -> Hash (8120.0)

self を返します。Hash クラスのサブクラスから呼び出した場合は self を Hash オブジェクトに変換します。

...変換します。

//emlist[例][ruby]{
hash = {}
p hash.to_h # => {}
p hash.to_h == hash # => true

class MyHash < Hash;end
my_hash = MyHash.new
p my_hash.to_h # => {}
p my_hash.class # => MyHash
p my_hash.to_h.class # => Hash
//}

ブロックを指定すると各ペアでブ...
...ロックを呼び出し、
その結果をペアとして使います。
//emlist[ブロック付きの例][ruby]{
hash = { "a" => 97, "b" => 98 }
hash.to_h {|key, value| [key.upcase, value-32] } # => {"A"=>65, "B"=>66}
//}

@see Enumerable#map...

Hash#values_at(*keys) -> [object] (8107.0)

引数で指定されたキーに対応する値の配列を返します。

...たキーに対応する値の配列を返します。

キーに対応する要素がなければデフォルト値が使用されます。

@param keys キーを 0 個以上指定します。

@return 引数で指定されたキーに対応する値の配列を返します。
引数が指...
...定されなかった場合は、空の配列を返します。

//emlist[例][ruby]{
h = {1=>"a", 2=>"b", 3=>"c"}

p h.values_at(1,3,4) #=> ["a", "c", nil]
# [h[1], h[3] ,h[4]] と同じ
//}

@see Hash#[] , Hash.new, Hash#default, Hash#default_proc, Array#values_at...
<< 1 2 > >>