Created
September 30, 2010 15:32
-
-
Save ongaeshi/604761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# @file | |
# @brief p_classtree 改良版 | |
# @author ongaeshi | |
# @date 2010/09/30 | |
# アルファベットと演算子で表示する数を変える | |
ALPHABET_DISP_NUM = 5 | |
OPERATOR_DISP_NUM = 10 | |
def p_classtree(c) | |
unless c.is_a?(Class) | |
c = c.class | |
end | |
while (true) | |
puts c.name | |
break if (c == Object) | |
p_classtree_sub(c) | |
c = c.superclass | |
end | |
end | |
def p_classtree_sub(c) | |
# メソッドの一覧を得る | |
group = c.public_instance_methods(false).sort.partition { |m| m =~ /\w/ } | |
array = group.flatten | |
operator_start_index = group[0].size | |
limit = ALPHABET_DISP_NUM | |
print (array.size > limit) ? "| " : "↓ " | |
counter = 0 | |
array.each_with_index do |v, index| | |
if (index == operator_start_index) | |
limit = OPERATOR_DISP_NUM | |
counter = 0 | |
puts | |
print (array.size - index > limit) ? "| " : "↓ " | |
end | |
if (counter >= limit) | |
counter = 0 | |
puts | |
print (array.size - index > limit) ? "| " : "↓ " | |
end | |
print v + ", " | |
counter += 1 | |
end | |
puts | |
end | |
if __FILE__ == $0 | |
# インスタンスを渡した場合はその所属するクラスの階層、メソッドを表示 | |
a = [0, 1, 2] | |
p_classtree(a) | |
puts | |
# クラス名を直接渡してもOK | |
p_classtree(Fixnum) | |
puts | |
p_classtree(Bignum) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment