8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rubyのhashは順序を保存する

Posted at

ruby-list MLを見ていたら「RubyのHashは順序を保存するので~」との一文が。
てっきり順不同だと思ってたので意外でしたが、試してみたらこんな感じ。
ちゃんと保存されてました。へーへーへー。

sample.rb
hash1 = {}
hash1[:key1] = :val1
hash1[:key2] = :val2
hash1[:key3] = :val3
p hash1 # {:key1=>:val1, :key2=>:val2, :key3=>:val3}

hash2 = {}
hash2[:key3] = :val3
hash2[:key2] = :val2
hash2[:key1] = :val1
p hash2 # {:key3=>:val3, :key2=>:val2, :key1=>:val1}

p hash1.keys # [:key1, :key2, :key3]
p hash2.keys # [:key3, :key2, :key1]

p hash1 == hash2 # true
8
8
2

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?