0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hash.new

Last updated at Posted at 2025-01-06

ブロックでないと同一のオブジェクトとしてみなされる

new(ifnone = nil) -> Hash[permalink][rdoc][edit]
空の新しいハッシュを生成します。ifnone はキーに対応する値が存在しない時のデフォルト値です。設定したデフォルト値はHash#defaultで参照できます。

ifnoneを省略した Hash.new は {} と同じです。

デフォルト値として、毎回同一のオブジェクトifnoneを返します。それにより、一箇所のデフォルト値の変更が他の値のデフォルト値にも影響します。

# ブロックではないデフォルト値は全部同一のオブジェクトなので、
# 破壊的変更によって他のキーに対応する値も変更されます。

irb(main):036> h = Hash.new("foo") # ifnoneを省略した Hash.new は {} と同じ
=> {}
irb(main):037> p h[1]
"foo"
=> "foo"
irb(main):038> h.size
=> 0
irb(main):039> h 
=> {}
irb(main):040> p h[1]
"foo"
=> "foo"
irb(main):041> p h.default # デフォルト値として、毎回同一のオブジェクトifnoneを返します
"foo"
=> "foo"
irb(main):042> h[1] << "bar"
=> "foobar"
irb(main):043> h[1] # 一箇所のデフォルト値の変更が他の値のデフォルト値にも影響
=> "foobar"
irb(main):044> h[2]
=> "foobar"
irb(main):045> h[3]
=> "foobar"
irb(main):046> h[1].object_id # object_idが同じ、同じオブジェクトという扱い
=> 313880
irb(main):047> h[2].object_id
=> 313880
irb(main):048> h[3].object_id
=> 313880
irb(main):060> h = Hash.new("foo")
=> {}
irb(main):061> h.nil?
=> false
irb(main):062> h = {}
=> {}
irb(main):063> h.nil?
=> false
irb(main):064> h.empty?
=> true
irb(main):065> h = Hash.new("foo")
=> {}
irb(main):066> h.empty?
=> true

参照するごとにブロックが実行される

new {|hash, key| ... } -> Hash
空の新しいハッシュを生成します。ブロックの評価結果がデフォルト値になります。設定したデフォルト値はHash#default_procで参照できます。

値が設定されていないハッシュ要素を参照するとその都度ブロックを実行し、その結果を返しますブロックにはそのハッシュとハッシュを参照したときのキーが渡されます

[EXCEPTION] ArgumentError:
ブロックと通常引数を同時に与えると発生します。

irb(main):001> h = Hash.new {|hash, key| hash[key] = "foo"}
=> {}
irb(main):002> h.default
=> nil
irb(main):049> h = Hash.new {|hash, key| hash[key] = "foo"}
=> {}
irb(main):050> h[1] #値が設定されていないハッシュ要素を参照するとその都度ブロックを実行し、その結果を返します
=> "foo" # ブロックにはそのハッシュとハッシュを参照したときのキーが渡されます
irb(main):051> h[1].object_id # 値が設定されている
=> 341640
irb(main):052> h
=> {1=>"foo"}
irb(main):053> h[1]
=> "foo"
irb(main):054> h[1] << "bar"
=> "foobar"
irb(main):055> h[1]
=> "foobar"
irb(main):056> h[1].object_id
=> 341640
irb(main):057> h[2]
=> "foo"
irb(main):058> h[2].object_id
=> 387960
irb(main):059> h
=> {1=>"foobar", 2=>"foo"}

値が設定されていないハッシュ要素を参照するとその都度ブロックを実行し、その結果を返します

irb(main):010> hash = Hash.new { [] }
irb(main):011> hash[:a] << 1
=> [1]
irb(main):012> hash[:a].object_id
=> 68820
irb(main):013> hash[:a].object_id
=> 70600
irb(main):014> hash
=> {}
irb(main):015> hash.default
=> nil
irb(main):016> hash[:a]
=> []
irb(main):017> hash[:a]
=> []
irb(main):018> hash = Hash.new { [] }
irb(main):019>
=> {}
irb(main):020> hash[:a]
=> []
irb(main):021> hash.size
=> 0

@namespaces = Hash.new { |h, k| h[k] = MethodSet.new(k) }

MethodSet.new(k)という値が設定される
以下のようになるので新しく参照すると新しくオブジェクトが生成される

irb(main):049> h = Hash.new {|hash, key| hash[key] = "foo"}
=> {}
irb(main):050> h[1] #値が設定されていないハッシュ要素を参照するとその都度ブロックを実行し、その結果を返します
=> "foo" # ブロックにはそのハッシュとハッシュを参照したときのキーが渡されます
irb(main):051> h[1].object_id # 値が設定されている
=> 341640
irb(main):052> h
=> {1=>"foo"}
irb(main):053> h[1]
=> "foo"
irb(main):054> h[1] << "bar"
=> "foobar"
irb(main):055> h[1]
=> "foobar"
irb(main):056> h[1].object_id
=> 341640
irb(main):057> h[2]
=> "foo"
irb(main):058> h[2].object_id
=> 387960
irb(main):059> h
=> {1=>"foobar", 2=>"foo"}

知るきっかけ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?