るりまサーチ

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

別のキーワード

  1. array fill
  2. array []
  3. array sample
  4. array new
  5. array []=

ライブラリ

キーワード

検索結果

Hash#assoc(key) -> Array | nil (210.0)

ハッシュが key をキーとして持つとき、見つかった要素のキーと値のペア を配列として返します。

...見つからなかった場合は、nil を返します。

@param key 検索するキー

//emlist[例][ruby]{
h = {"colors" => ["red", "blue", "green"],
"letters" => ["a", "b", "c" ]}
h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
h.assoc("foo") #=> nil
//}



@see Array#assoc...

Hash#rassoc(value) -> Array | nil (210.0)

ハッシュ内を検索して,引数 value と 一致する値を探します。

...とするサイズ 2 の配列を返します。
ない場合には nil を返します。

@param value 探索する値。

//emlist[例][ruby]{
a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
a.rassoc("two") #=> [2, "two"]
a.rassoc("four") #=> nil
//}

@see Hash#assoc, Array#rassoc...

Hash#shift -> [object, object] | nil (115.0)

ハッシュからキーが追加された順で先頭の要素をひとつ取り除き、 [key, value]という配列として返します。

...取り除かれた残りのハッシュに変更されます。

Ruby 3.2以前は、ハッシュが空の場合、デフォルト値(Hash#defaultまたはHash#default_procのブロックの値か、どちらもnilならばnil)
を返します(このとき、[key,value] という形式の値を...
..., "all"]
p h #=> {}
p h.shift #=> nil

h1 = Hash.new("default value")
p h1 #=> {}
p h1.shift #=> "default value"

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


@see Array#shift...
...#=> [:cd, "all"]
p h #=> {}
p h.shift #=> nil

h1 = Hash.new("default value")
p h1 #=> {}
p h1.shift #=> nil

h2 = Hash.new {|*arg| arg}
p h2 #=> {}
p h2.shift #=> nil
//}


@see Array#shift...

Hash#compact! -> self | nil (109.0)

compact は自身から value が nil のもの取り除いた Hash を生成して返します。 compact! は自身から破壊的に value が nil のものを取り除き、変更が行われた場合は self を、そうでなければ nil を返します。

... Hash を生成して返します。 compact! は自身から破壊的に value が nil のものを取り除き、変更が行われた場合は self を、そうでなければ nil を返します。


//emlist[例][ruby]{
hash
= {a: 1, b: nil, c: 3}
p hash.compact #=> {:a=>1, :c=>3}
p hash...
...#=> {:a=>1, :b=>nil, :c=>3}
hash
.compact!
hash
#=> {:a=>1, :c=>3}
p hash.compact! #=> nil
//}

@see Array#compact...

Hash#dig(key, ...) -> object | nil (109.0)

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

...します。

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

//emlist[例][ruby]{
h = { foo: {bar: {baz: 1}}}

h.dig(:foo, :bar, :baz) # => 1
h.dig(:foo, :zot, :xyz) # => nil

g = { foo: [10, 11, 12] }
g.dig(:foo, 1) # => 11
//}

@see Array#dig, Struct#dig, OpenStruct#dig...

絞り込み条件を変える

Hash#compact -> Hash (9.0)

compact は自身から value が nil のもの取り除いた Hash を生成して返します。 compact! は自身から破壊的に value が nil のものを取り除き、変更が行われた場合は self を、そうでなければ nil を返します。

... Hash を生成して返します。 compact! は自身から破壊的に value が nil のものを取り除き、変更が行われた場合は self を、そうでなければ nil を返します。


//emlist[例][ruby]{
hash
= {a: 1, b: nil, c: 3}
p hash.compact #=> {:a=>1, :c=>3}
p hash...
...#=> {:a=>1, :b=>nil, :c=>3}
hash
.compact!
hash
#=> {:a=>1, :c=>3}
p hash.compact! #=> nil
//}

@see Array#compact...