24件ヒット
[1-24件を表示]
(0.074秒)
検索結果
-
Hash
. new {|hash , key| . . . } -> Hash (8.0) -
空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になりま す。設定したデフォルト値はHash#default_procで参照できます。
...#=> "foo"
p h[2].object_id #=> 6126840
p h #=> {1=>"foobar", 2=>"foo"}
# 値が設定されていないときに(fetchのように)例外をあげるようにもできる
h = Hash.new {|hash, key|
raise(IndexError, "hash[#{key}] has no value")... -
Hash
. new(ifnone = nil) -> Hash (8.0) -
空の新しいハッシュを生成します。ifnone はキーに対 応する値が存在しない時のデフォルト値です。設定したデフォルト値はHash#defaultで参照できます。
...#=> ["bar"]
p h[1] #=> ["bar"]
p h[2] #=> ["bar"]
p h[2].object_id #=> 6127150
p h #=> {}
h = Hash.new([].freeze)
h[0] += [0] #破壊的でないメソッドはOK
h[1] << 1
# エラー: can't modify frozen Array (RuntimeError)
//}......#=> ["bar"]
p h[1] #=> ["bar"]
p h[2] #=> ["bar"]
p h[2].object_id #=> 6127150
p h #=> {}
h = Hash.new([].freeze)
h[0] += [0] #破壊的でないメソッドはOK
h[1] << 1
# エラー: can't modify frozen Array (FrozenError)
//}...