Created
August 26, 2012 11:33
-
-
Save atton/3477803 to your computer and use it in GitHub Desktop.
hash and array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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