-
Notifications
You must be signed in to change notification settings - Fork 2
/
ruby-multi-hash.html
127 lines (93 loc) · 3.64 KB
/
ruby-multi-hash.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="description" content="Akkunchoi.github.com : " />
<link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/rainbow-github.css">
<link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/stylesheet.css">
<title>Ruby 多次元ハッシュ | akkunchoi@github</title>
<link rel="alternate" type="application/rss+xml" title="RSS Feed for mysite.com" href="/feed.xml" />
<script type="text/javascript" src="/javascripts/rainbow-custom.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function($){
//$('pre > code').highlight({source: 1, zebra: 1, indent: 'space', list: 'ol'});
});
</script>
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/akkunchoi">View on GitHub</a>
<h1 id="project_title">
<a href="/">akkunchoi.github.com</a>
</h1>
<h2 id="project_tagline"></h2>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<article class="posts">
<header>
<h1>Ruby 多次元ハッシュ</h1>
<ul class="tags">
<li class="inline archive_list"><a class="tag_list_link" href="/tag/ruby">ruby</a></li>
</ul>
</header>
<div class="posts-content">
<p>PHPでこんなことをRubyでもやりたい。</p>
<pre><code>$a = array();
$a['foo']['bar']['fuga'] = 'hoge';
</code></pre>
<p>PHPのノリでやるとこうなる。</p>
<pre><code>> a = {}
=> {}
> a['foo']['bar'] = 'hoge'
NoMethodError: undefined method `[]' for nil:NilClass
</code></pre>
<p>Hashの場合、キーが存在しない場合の処理、というtrap::Hashなるものが作成できる。</p>
<pre><code># 存在しない場合の処理をブロックで渡す
> a = Hash.new{ |hash,key| hash[key] = {} }
=> {}
# foo は存在しないキーだが、ここで先ほどのブロックが発動
# a['foo'] = {} が呼び出された後に処理が実行されるのでエラーが発生しない
#
> a['foo']['bar'] = 'hoge'
=> "hoge"
> a
=> {"foo"=>{"bar"=>"hoge"}}
</code></pre>
<p>しかし多次元になる場合、Arrayの場合はめんどくさい</p>
<ul>
<li><a href="http://www.ruby-lang.org/ja/old-man/html/Hash.html">http://www.ruby-lang.org/ja/old-man/html/Hash.html</a></li>
<li><a href="http://k.try-z.com/?p=264">http://k.try-z.com/?p=264</a></li>
</ul>
</div>
<footer>
<p>Last updated at <time>2012-09-23 13:52:29 +0900</time></p>
<!-- <p>Published at <time>2012-06-29</time></p> -->
</footer>
</article>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p>Published with <a href="http://pages.github.com">GitHub Pages</a></p>
</footer>
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36469923-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>