[#44757] [ruby-trunk - Bug #5533][Assigned] Pathname's raising "invalid sequence" — Shyouhei Urabe <shyouhei@...>
5 messages
2011/11/01
[#44775] [ruby-trunk - Feature #5549][Open] Comparable#min, Comparable#max — Tsuyoshi Sawada <sawadatsuyoshi@...>
4 messages
2011/11/02
[#44796] [Backport93 - Backport #5565][Open] STDOUT.puts writes LF (not CR+LF) in textmode — Usaku NAKAMURA <redmine@...>
6 messages
2011/11/03
[#44800] [ruby-trunk - Bug #5576][Open] [Universal Newline]改行コードが"\r\n"で出力されない — pegacorn jp <subscriber.jp+ruby-redmine@...>
8 messages
2011/11/06
[#44817] [ruby-trunk - Feature #5584][Open] Array#sample! — Masaki Matsushita <glass.saga@...>
12 messages
2011/11/07
[#44837] [ruby-trunk - Bug #5593][Open] Windows上で \\.\ から始まるデバイスをopenするとクラッシュ — 宗介 相良 <snowjail@...>
5 messages
2011/11/08
[#44853] [ruby-trunk - Bug #5608][Open] make -j 16 sometimes fail — Tomoaki Nishiyama <tomoakin@...>
5 messages
2011/11/10
[#44870] Re: [ruby-cvs:40906] kosaki:r33728 (trunk): * test/ruby/test_io.rb (test_fcntl_dupfd): skip if Fcntl::DUPFD — Tanaka Akira <akr@...>
2011/11/13 <kosaki@ruby-lang.org>:
3 messages
2011/11/14
[#44899] Re: [ruby-cvs:41015] kosaki:r33837 (trunk): * configure.in: turn on PIE if --enable-shared is not specified. — Tanaka Akira <akr@...>
2011/11/25 <kosaki@ruby-lang.org>:
3 messages
2011/11/27
[#44904] [ruby-trunk - Bug #5688][Open] Solaris10 で spawn を繰り返すとメモリリークする — okkez _ <redmine@...>
14 messages
2011/11/29
[#44922] [ruby-trunk - Bug #5688] Solaris10 で spawn を繰り返すとメモリリークする
— okkez _ <redmine@...>
2011/12/02
[#44968] [ruby-trunk - Bug #5688] Solaris10 で spawn を繰り返すとメモリリークする
— okkez _ <redmine@...>
2011/12/12
[#44906] [ruby-trunk - Bug #5689][Open] class variables inside of instance methods inside of "class_eval" — Shyouhei Urabe <shyouhei@...>
5 messages
2011/11/30
[ruby-dev:44799] [ruby-trunk - Feature #5550] Hash#depth, Hash#flat_length for recursive hashes
From:
Alexey Muranov <muranov@...>
Date:
2011-11-05 15:46:13 UTC
List:
ruby-dev #44799
Issue #5550 has been updated by Alexey Muranov.
Excuse me, can you be more precise with your example please? Ruby does not accept it (after removing the dots "...")? Are you talking about nested hashes? How about creating a class Tree that would inherit from Hash and define additional methods there?
----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
http://redmine.ruby-lang.org/issues/5550
Author: Tsuyoshi Sawada
Status: Feedback
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://redmine.ruby-lang.org