{"id":1082,"date":"2013-09-13T17:09:18","date_gmt":"2013-09-13T21:09:18","guid":{"rendered":"http:\/\/www.matthewgkeller.com\/blog\/?p=1082"},"modified":"2013-09-13T11:18:10","modified_gmt":"2013-09-13T15:18:10","slug":"ruby-hash-extensions","status":"publish","type":"post","link":"http:\/\/www.matthewgkeller.com\/blog\/2013\/09\/13\/ruby-hash-extensions\/","title":{"rendered":"Ruby Hash Extensions"},"content":{"rendered":"<p>As I <a title=\"Ruby String.each\" href=\"http:\/\/www.matthewgkeller.com\/blog\/2013\/08\/01\/ruby-string-each\/\">mentioned before<\/a>, one of the nice things about Ruby is its malleability. Here are a couple extensions to the Ruby Hash object:<\/p>\n<pre>class ::Hash\r\n  def random\r\n    return self.keys[rand(self.size)]\r\n  end\r\n\r\n  def byValue(subKey)\r\n      if subKey\r\n        self.sort_by {|key, value| value[subKey]}.reverse\r\n      else\r\n        self.sort_by {|key, value| value}.reverse\r\n      end\r\n  end\r\nend<\/pre>\n<p>Simply, Hash.random will now return a random hash key. Hash.byValue will sort the hash by value. Hash.byValue(&#8216;priority&#8217;) will sort a Hash-of-Hashes by the value of the named sub-key. TIMTOWTDI, for sure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I mentioned before, one of the nice things about Ruby is its malleability. Here are a couple extensions to the Ruby Hash object: class ::Hash def random return self.keys[rand(self.size)] end def byValue(subKey) if subKey self.sort_by {|key, value| value[subKey]}.reverse else &hellip; <a href=\"http:\/\/www.matthewgkeller.com\/blog\/2013\/09\/13\/ruby-hash-extensions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[100],"class_list":["post-1082","post","type-post","status-publish","format-standard","hentry","category-general-coding","category-linuxy","tag-ruby"],"_links":{"self":[{"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/posts\/1082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/comments?post=1082"}],"version-history":[{"count":0,"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/posts\/1082\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/media?parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/categories?post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.matthewgkeller.com\/blog\/wp-json\/wp\/v2\/tags?post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}