{"provider_url":"https://hatena.blog","width":"100%","blog_title":"k\u306a\u3093\u3068\u304b\u306e\u65e5\u8a18","author_name":"kwatch","author_url":"https://blog.hatena.ne.jp/kwatch/","published":"2008-07-14 01:05:22","description":"ActiveSupport \u3067\u7528\u610f\u3055\u308c\u3066\u3044\u308b Enumerable#index_by() \u304cRuby\u672c\u4f53\u306b\u3068\u308a\u3053\u307e\u308c\u306a\u3044\u304b\u306a\u30fc\u3002 \u3053\u308c\u3001\u307b\u3093\u3068\u3088\u304f\u4f7f\u3046\u308f\u3002 module Enumerable def index_by() # \u540d\u524d\u306f to_hash \u306e\u307b\u3046\u304c\u597d\u307f hash = {} each do |item| key = yield(item) hash[key] = item end return hash end end ## example: \u30ad\u30fc\u304c user id, \u5024\u304c User \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3042\u308b\u3088\u3046\u306a Hash \u3092\u4f5c\u308b hash = User.find(:all).index_\u2026","provider_name":"Hatena Blog","url":"https://kwatch.hatenadiary.org/entry/20080714/1216051522","title":" Enumerable#index_by() \u304c\u6a19\u6e96\u3067\u6b32\u3057\u3044","type":"rich","image_url":null,"version":"1.0","html":"<iframe src=\"https://hatenablog-parts.com/embed?url=https%3A%2F%2Fkwatch.hatenadiary.org%2Fentry%2F20080714%2F1216051522\" title=\" Enumerable#index_by() \u304c\u6a19\u6e96\u3067\u6b32\u3057\u3044 - k\u306a\u3093\u3068\u304b\u306e\u65e5\u8a18\" class=\"embed-card embed-blogcard\" scrolling=\"no\" frameborder=\"0\" style=\"display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;\"></iframe>","categories":["ruby"],"height":"190","blog_url":"https://kwatch.hatenadiary.org/"}