Skip to content

Instantly share code, notes, and snippets.

@kazoo04
Created March 20, 2014 17:10
Show Gist options
  • Save kazoo04/9668898 to your computer and use it in GitHub Desktop.
Save kazoo04/9668898 to your computer and use it in GitHub Desktop.
# encoding: utf-8
dic = {'朝' => 'first', '昼' => 'second', '夜' => 'third'}
# キーから対応する値を取れる
# 逆(値からキーを取る)はできない
puts dic['朝'] # first
puts dic['昼'] # second
puts dic['夜'] # third
# each を使うとすべてのキーと値の組み合わせを列挙できる
# (eachのブロックがキーの数だけ繰り返される)
dic.each { |key, value|
puts "key = #{key}, value = #{value}"
}
# キーは1つしかないので上書きされる
dic['朝'] = 'fourth'
puts dic['朝'] # fourth と出力される
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment