From: "mame (Yusuke Endoh)" Date: 2012-11-20T20:59:51+09:00 Subject: [ruby-dev:46555] [ruby-trunk - Feature #5550][Rejected] Hash#depth, Hash#flat_length for recursive hashes Issue #5550 has been updated by mame (Yusuke Endoh). Status changed from Feedback to Rejected No feedback, looks hopeless to me. Closing. -- Yusuke Endoh ---------------------------------------- Feature #5550: Hash#depth, Hash#flat_length for recursive hashes https://bugs.ruby-lang.org/issues/5550#change-33202 Author: sawa (Tsuyoshi Sawada) Status: Rejected Priority: Normal Assignee: Category: Target version: I often have a hash whose value is recursively a hash, which may look like the following: {"Japan" => {"Hokkaido" => "Sapporo", ...}, {"Honhuu" => {"Aomori" => "Hirosaki", ...}, {"Akita" => ...}, ... }, {"Shikoku" => ...}, ... } In these cases, it will be convenient if there is a way to know the (maximum) depth of he original hash, and the numbers of all the "terminal nodes". I would like to propose two methods Hash#depth and Hash#flat_length, whose Ruby implementation can be as follows: class Hash def depth 1 + (values.map{|v| Hash === v ? v.depth : 1}.max) end def flat_length values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)} end end -- http://bugs.ruby-lang.org/