Skip to content

Instantly share code, notes, and snippets.

@atton
Created August 26, 2012 11:33
Show Gist options
  • Save atton/3477803 to your computer and use it in GitHub Desktop.
Save atton/3477803 to your computer and use it in GitHub Desktop.
hash and array
# -*- coding: utf-8 -*-
# ハッシュの中に配列
array_in_hash = {:hoge => [1,2] , :fuga => [3,4]}
# keyを渡すと配列が返ってくる
p array_in_hash[:hoge] # => [1, 2]
# その配列の0番目
p array_in_hash[:hoge][0] # => 1
# 配列の中にハッシュ
hash_in_array = [ {:hoge => 10, :fuga => 20} , {:hoge => 15, :fuga => 25}]
# 添字を渡すとハッシュが返ってくる
p hash_in_array[1] # => {:hoge=>15, :fuga=>25}
# そのハッシュにkeyを渡す
p hash_in_array[1][:fuga] # => 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment