るりまサーチ

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

別のキーワード

  1. openssl t61string
  2. asn1 t61string
  3. matrix t
  4. t61string new
  5. fiddle type_size_t

ライブラリ

キーワード

検索結果

Hash#invert -> Hash (6126.0)

値からキーへのハッシュを作成して返します。

...list[例][ruby]{
h = { "a" => 0, "b" => 100, "c" => 200, "d" => 300, "e" => 300 }
p h.invert #=> {0=>"a", 100=>"b", 200=>"c", 300=>"e"}
//}

=== 参考
値が重複していたときに備えて、変換後の値を配列として保持するには、次のようにします。

//emlist[][ruby]{...
...def safe_invert(orig_hash)
orig_hash.each_key.group_by do |key|
orig_hash[key]
end

end

p safe_invert({"a"=>1, "b"=>1, "c"=>3}) # => {1=>["a", "b"], 3=>["c"]}
//}

@see Hash#key...

Hash#default_proc=(pr) (6120.0)

ハッシュのデフォルト値を返す Proc オブジェクトを 変更します。

...デフォルトは値(Hash#default)の場合も
Proc の場合(Hash#default_proc)でも上書きされます。

引数には to_proc で Proc オブジェクトに変換できる
オブジェクトも受け付けます。

nil を指定した場合は現在の Hash#default_proc をクリアしま...
...す。

@param pr デフォルト値を返す手続きオブジェクト

//emlist[例][ruby]{
h = {}
h.default_proc = proc do |hash, key|
hash
[key] = case
when (key % 15).zero?
"FizzBuzz"
when (key % 5).zero?
"Buzz"
when (...
...end
end

p h[1] # => 1
p h[2] # => 2
p h[3] # => "Fizz"
p h[5] # => "Buzz"
p h[15] # => "FizzBuzz"

h.default_proc = nil
p h[16] # => nil
# default_proc が nil になったので `16=>16 が追加されていない`
p h # => {1=>1, 2=>2, 3=>"Fizz", 5=>"Buzz", 15=>"FizzBuzz"}
//}

@see Hash#d...

Hash#to_h -> self | Hash (6120.0)

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

...す。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.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}

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

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

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

...す。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.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}

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

Hash#to_h -> self | Hash (6114.0)

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

...す。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.clas...
...s # => MyHash
p my_hash.to_h.class # => Hash
//}...

絞り込み条件を変える

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

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

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

self と others に同じキーがあった場合はブロック付きか否かで
判定方法が違います。ブロック付きのときはブ...
...常に others の値を使います。

othersがハッシュではない場合、othersのメソッドto_hashを使って暗黙の変換を試みます。

@param others マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マ...
...ージした結果を返します

//emlist[][ruby]{
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 246, "c" => 300 }
h3 = { "b" => 357, "d" => 400 }
h1.merge #=> {"a"=>100, "b"=>200}
h1.merge(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d...

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

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

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

self と other に同じキーがあった場合はブロック付きか否かで
判定方法が違います。ブロック付きのときはブロックを...
...常に other の値を使います。

otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

@param other マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マー...
...ジした結果を返します

//emlist[][ruby]{
h1 = { "a" => 100, "b" => 200 }
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, "...

Hash#merge(*others) -> Hash (132.0)

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

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

self と others に同じキーがあった場合はブロック付きか否かで
判定方法が違います。ブロック付きのときはブ...
...常に others の値を使います。

othersがハッシュではない場合、othersのメソッドto_hashを使って暗黙の変換を試みます。

@param others マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マ...
...ージした結果を返します

//emlist[][ruby]{
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 246, "c" => 300 }
h3 = { "b" => 357, "d" => 400 }
h1.merge #=> {"a"=>100, "b"=>200}
h1.merge(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d...

Hash#merge(other) -> Hash (132.0)

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

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

self と other に同じキーがあった場合はブロック付きか否かで
判定方法が違います。ブロック付きのときはブロックを...
...常に other の値を使います。

otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

@param other マージ用のハッシュまたはメソッド to_hash でハッシュに変換できるオブジェクトです。
@return マー...
...ジした結果を返します

//emlist[][ruby]{
h1 = { "a" => 100, "b" => 200 }
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, "...

Hash#replace(other) -> self (120.0)

ハッシュの内容を other の内容で置き換えます。

...の内容を other の内容で置き換えます。

デフォルト値の設定もotherの内容になります。
otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。

self = other.to_hash.dup と同じです。

@param other ハッシ...
... to_hash でハッシュに変換できるオブジェクトです。
@return self を返します。

//emlist[例][ruby]{
foo = {1 => 'a', 2 => 'b'}
bar = {2 => 'B', 3 => 'C'}

foo.replace(bar)
p foo #=> {2=>"B", 3=>"C"}

zoo = {}
zoo = bar.dup
p zoo #=> {2=>"B", 3=>"C"}

class Foo
def to_hash...
...{:japan => 'kyoto'}
end

end


h = Hash.new
h.replace(Foo.new) #暗黙の変換
p h #=> {:japan=>"kyoto"}
//}

@see Hash#dup,Hash#merge,Object#to_hash...

絞り込み条件を変える