{"id":1923,"date":"2013-02-06T09:11:18","date_gmt":"2013-02-06T00:11:18","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1923"},"modified":"2018-11-14T16:33:56","modified_gmt":"2018-11-14T07:33:56","slug":"ruby-rails-map%e3%81%a7%e6%99%82%e9%96%93%e3%82%92%e4%bf%9d%e6%8c%81%e3%81%99%e3%82%8bhash%e3%82%92%e4%bd%9c%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/02\/06\/ruby-rails-map%e3%81%a7%e6%99%82%e9%96%93%e3%82%92%e4%bf%9d%e6%8c%81%e3%81%99%e3%82%8bhash%e3%82%92%e4%bd%9c%e3%82%8b\/","title":{"rendered":"Ruby\/Rails: Enumerable#map \u3067\u6642\u9593\u3092\u4fdd\u6301\u3059\u308b Hash \u3092\u4f5c\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails \u306e View \u3067\u6642\u9593\u3092\u9078\u629e\u3059\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u3092\u4f5c\u308a\u305f\u3044\u5834\u5408\u306b\u306f\u3001collection_select \u306a\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>collection_select \u3067\u4f7f\u7528\u3059\u308b\u300c\u6642\u9593\u3092\u4fdd\u6301\u3059\u308b Hash\u300d\u3067\u3059\u304c\u3001\u3069\u306e\u3088\u3046\u306b\u4f5c\u308a\u307e\u3059\u304b\uff1f<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a HOURS \u3068\u3044\u3046 Hash \u304c\u5b9a\u7fa9\u3055\u308c\u3066\u304a\u308a\u3001<\/p>\n<pre class=\"lang:ruby decode:true\">HOURS = {0 =&gt; '0\u6642', 1 =&gt; '1\u6642', 2 =&gt; '2\u6642', ..., 23 =&gt; '23\u6642'}<\/pre>\n<p>View \u3067\u306f\u6b21\u306e\u3088\u3046\u306b\u4f7f\u7528\u3059\u308b\u60f3\u5b9a\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&lt;%= form.collection_select(:hour, HOURS, :first, :last) %&gt;<\/pre>\n<p>\u3053\u3053\u3067\u306e HOURS \u3092\u3069\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3057\u307e\u3059\u304b\u3001\u3068\u3044\u3046\u8a71\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u611a\u76f4\u306b\u66f8\u3044\u3066\u307f\u308b<\/h2>\n<p>\u8a66\u3057\u306b \u611a\u76f4\u306b\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">HOURS = {0=&gt;\"0\u6642\", 1=&gt;\"1\u6642\", 2=&gt;\"2\u6642\", 3=&gt;\"3\u6642\", 4=&gt;\"4\u6642\", 5=&gt;\"5\u6642\",\r\n         6=&gt;\"6\u6642\", 7=&gt;\"7\u6642\", 8=&gt;\"8\u6642\", 9=&gt;\"9\u6642\", 10=&gt;\"10\u6642\", 11=&gt;\"11\u6642\",\r\n         12=&gt;\"12\u6642\", 13=&gt;\"13\u6642\", 14=&gt;\"14\u6642\", 15=&gt;\"15\u6642\", 16=&gt;\"16\u6642\", 17=&gt;\"17\u6642\",\r\n         18=&gt;\"18\u6642\", 19=&gt;\"19\u6642\", 20=&gt;\"20\u6642\", 21=&gt;\"21\u6642\", 22=&gt;\"22\u6642\", 23=&gt;\"23\u6642\"}<\/pre>\n<p>\u5c11\u3057\u30dc\u30ea\u30e5\u30fc\u30e0\u304c\u3042\u308a\u3059\u304e\u308b\u611f\u3058\u304c\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Enumerable#map \u3068 Hash[] \u3092\u4f7f\u3046<\/h2>\n<p>Enumerable#map \u3068 Hash[] \u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">HOURS = Hash[(0..23).map{|x| [x, sprintf('%d\u6642', x)]}]<\/pre>\n<p>1 \u884c\u306b\u307e\u3068\u307e\u308a\u3001\u3059\u3063\u304d\u308a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u30dd\u30a4\u30f3\u30c8\u306f (0..23) \u3092 map \u3057\u3066\u3001\u300c[[0, '0\u6642'], [1, '1\u6642'], [2, '2\u6642\"], ..., [23, '23\u6642']]\u300d\u3068\u3044\u3046\u914d\u5217\u3092\u4f5c\u6210\u3057\u3001\u305d\u308c\u3092 Hash[] \u306b\u6e21\u3057\u3066\u3044\u308b\u70b9\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5206\u3092\u4fdd\u6301\u3059\u308b Hash<\/h2>\n<p>\u4eca\u5ea6\u306f\u300c\u5206\u300d\u3092\u4fdd\u6301\u3059\u308b Hash \u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a 0 \u5206\u304b\u3089 59 \u5206\u307e\u3067\u306e\u6642\u9593\u3092\u4fdd\u6301\u3059\u308b\u5b9a\u6570 MINUTES \u3092\u521d\u671f\u5316\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">MINUTES # =&gt; {0=&gt;\"0\u5206\", 1=&gt;\"1\u5206\", ..., 59=&gt;\"59\u5206\"}<\/pre>\n<p>\u3084\u306f\u308a View \u3067\u6642\u9593\u3092\u9078\u629e\u3059\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u3092\u4f5c\u308b\u3068\u304d\u306b\u4f7f\u7528\u3059\u308b\u60f3\u5b9a\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&lt;%= form.collection_select(:minute, MINUTES, :first, :last) %&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u611a\u76f4\u306b\u66f8\u3044\u3066\u307f\u308b\uff08\uff12\uff09<\/h2>\n<p>\u3068\u308a\u3042\u3048\u305a\u611a\u76f4\u306b\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">MINUTES = {0=&gt;\"0\u5206\", 1=&gt;\"1\u5206\", 2=&gt;\"2\u5206\", 3=&gt;\"3\u5206\", 4=&gt;\"4\u5206\", 5=&gt;\"5\u5206\",\r\n           6=&gt;\"6\u5206\", 7=&gt;\"7\u5206\", 8=&gt;\"8\u5206\", 9=&gt;\"9\u5206\", 10=&gt;\"10\u5206\", 11=&gt;\"11\u5206\",\r\n           12=&gt;\"12\u5206\", 13=&gt;\"13\u5206\", 14=&gt;\"14\u5206\", 15=&gt;\"15\u5206\", 16=&gt;\"16\u5206\", 17=&gt;\"17\u5206\",\r\n           18=&gt;\"18\u5206\", 19=&gt;\"19\u5206\", 20=&gt;\"20\u5206\", 21=&gt;\"21\u5206\", 22=&gt;\"22\u5206\", 23=&gt;\"23\u5206\",\r\n           24=&gt;\"24\u5206\", 25=&gt;\"25\u5206\", 26=&gt;\"26\u5206\", 27=&gt;\"27\u5206\", 28=&gt;\"28\u5206\", 29=&gt;\"29\u5206\",\r\n           30=&gt;\"30\u5206\", 31=&gt;\"31\u5206\", 32=&gt;\"32\u5206\", 33=&gt;\"33\u5206\", 34=&gt;\"34\u5206\", 35=&gt;\"35\u5206\",\r\n           36=&gt;\"36\u5206\", 37=&gt;\"37\u5206\", 38=&gt;\"38\u5206\", 39=&gt;\"39\u5206\", 40=&gt;\"40\u5206\", 41=&gt;\"41\u5206\",\r\n           42=&gt;\"42\u5206\", 43=&gt;\"43\u5206\", 44=&gt;\"44\u5206\", 45=&gt;\"45\u5206\", 46=&gt;\"46\u5206\", 47=&gt;\"47\u5206\",\r\n           48=&gt;\"48\u5206\", 49=&gt;\"49\u5206\", 50=&gt;\"50\u5206\", 51=&gt;\"51\u5206\", 52=&gt;\"52\u5206\", 53=&gt;\"53\u5206\",\r\n           54=&gt;\"54\u5206\", 55=&gt;\"55\u5206\", 56=&gt;\"56\u5206\", 57=&gt;\"57\u5206\", 58=&gt;\"58\u5206\", 59=&gt;\"59\u5206\"}<\/pre>\n<p>\u6642\u9593\uff080\u301c23\uff09\u3088\u308a\u3082\u5206\uff080\u301c59\uff09\u306e\u65b9\u304c\u8981\u7d20\u6570\u304c\u591a\u3044\u3060\u3051\u306b\u3001\u3071\u3063\u3068\u898b\u305f\u3060\u3051\u3067\u5727\u8feb\u611f\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Enumerable#map \u3068 Hash[] \u3092\u4f7f\u3046\uff08\uff12\uff09<\/h2>\n<p>\u6642\u9593\u3092\u4fdd\u6301\u3059\u308b Hash \u306e\u3068\u304d\u3068\u540c\u69d8\u306b\u3001Enumerable#map \u3068 Hash[] \u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">MINUTES = Hash[(0..59).map{|x| [x, sprintf('%d\u5206', x)]}]<\/pre>\n<p>\u3084\u306f\u308a 1 \u884c\u306b\u307e\u3068\u307e\u308a\u3001\u975e\u5e38\u306b\u3059\u3063\u304d\u308a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>5 \u5206\u9593\u9694\u306e\u5206\u3092\u4fdd\u6301\u3059\u308b Hash<\/h2>\n<p>\u3061\u3087\u3063\u3068\u3057\u305f\u5fdc\u7528\u554f\u984c\u3067\u3059\u3002<\/p>\n<p>\u5148\u7a0b\u306f 1 \u5206\u9593\u9694\u306e\u5206\u3092\u4fdd\u6301\u3059\u308b Hash \u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u304c\u30015 \u5206\u9593\u9694\u306e\u5834\u5408\u306f\u3069\u3046\u3057\u307e\u3059\u304b\uff1f<\/p>\n<p>\u3053\u306e\u5834\u5408\u3001(0..59) \u3092 map \u3059\u308b\u524d\u306b\u3001select \u3067\u4e0d\u8981\u306a\u8981\u7d20\u3092\u9664\u5916\u3059\u308c\u3070 OK \u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">MINUTES = Hash[(0..59).select{|x| x % 5 == 0}.map{|x| [x, sprintf('%d\u5206', x)]}]<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u666e\u6bb5 Ruby \u3092\u4f7f\u308f\u308c\u3066\u3044\u308b\u65b9\u306b\u3068\u3063\u3066\u306f\u7c21\u5358\u306a\u5185\u5bb9\u3060\u3063\u305f\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e00\u65b9\u3067\u3001\u5197\u9577\u306a\u66f8\u304d\u65b9\u306e\u305b\u3044\u3067\u898b\u3065\u3089\u304f\u306a\u3063\u3066\u3044\u308b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u898b\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u8a00\u8a9e\u3084\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u30ec\u30fc\u30eb\u306b\u4e57\u308b\u305f\u3081\u306e\u57fa\u790e\u4f53\u529b\u304c\u91cd\u8981\u3060\u306a\u3068\u601d\u3046\u4eca\u65e5\u3053\u306e\u3054\u308d\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Rails \u306e View \u3067\u6642\u9593\u3092\u9078\u629e\u3059\u308b\u30d7\u30eb\u30c0\u30a6\u30f3\u3092\u4f5c\u308a\u305f\u3044\u5834\u5408\u306b\u306f\u3001collection_select \u306a\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/02\/06\/ruby-rails-map%e3%81%a7%e6%99%82%e9%96%93%e3%82%92%e4%bf%9d%e6%8c%81%e3%81%99%e3%82%8bhash%e3%82%92%e4%bd%9c%e3%82%8b\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1923"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1923"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1923\/revisions"}],"predecessor-version":[{"id":17961,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1923\/revisions\/17961"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}