読者です 読者をやめる 読者になる 読者になる

Rubyのクラス階層やメソッドを美しく表示する、rubywho

ruby

以前作ったものをgemにまとめました。実行中のオブジェクトのクラス階層や、メソッド一覧を簡単に調べることが出来ます。

require 'rubygems'
require 'rubywho'

# Hello.
1.who?
"string".who?
String.who?
[1, 2, 3].who?

# Method filtering.
require 'active_record'
ActiveRecord::Base.who?(/sql/)

実行結果

Fixnumのクラス階層

== 1.who? ==
Fixnum#
| abs, div, divmod, even?, fdiv, id2name, modulo, odd?, power!, quo, rdiv
| rpower, size, to_f, to_s, to_sym, zero?
| %, &, *, **, +, -, -@, /, <, <<, <=, <=>, ==, >, >=, >>, [], ^, |, ~
v-------------------------------------------------------------------------------
Integer#
| ceil, chr, denominator, downto, even?, floor, gcd, gcdlcm, integer?, lcm, next
| numerator, odd?, ord, pred, round, succ, taguri, taguri=, times, to_bn, to_i
| to_int, to_r, to_yaml, truncate, upto
v-------------------------------------------------------------------------------
Precision#
| prec, prec_f, prec_i
v-------------------------------------------------------------------------------
Numeric#
| abs, ceil, coerce, div, divmod, eql?, fdiv, floor, integer?, modulo, nonzero?
| quo, remainder, round, singleton_method_added, step, to_int, truncate, zero?
| +@, -@, <=>
v-------------------------------------------------------------------------------
Comparable#
| between?
| <, <=, ==, >, >=
v-------------------------------------------------------------------------------
Object#
| taguri, taguri=, to_yaml, to_yaml_properties, to_yaml_style
v-------------------------------------------------------------------------------
Kernel#
| __id__, __send__, class, clone, display, dup, enum_for, eql?, equal?, extend
| freeze, frozen?, hash, id, inspect, instance_eval, instance_exec, instance_of?
| instance_variable_defined?, instance_variable_get, instance_variable_set
| instance_variables, is_a?, kind_of?, method, methods, nil?, object_id
| private_methods, protected_methods, public_methods, respond_to?, send
| singleton_methods, taint, tainted?, tap, to_a, to_enum, to_s, type, untaint
| ==, ===, =~
v-------------------------------------------------------------------------------

Stringのクラス階層

== "string".who? ==
String#
| bytes, bytesize, capitalize, capitalize!, casecmp, center, chars, chomp
| chomp!, chop, chop!, concat, count, crypt, delete, delete!, downcase
| downcase!, dump, each, each_byte, each_char, each_line, empty?, end_with?
| eql?, gsub, gsub!, hash, hex, include?, index, insert, inspect, intern
| is_binary_data?, is_complex_yaml?, length, lines, ljust, lstrip, lstrip!
| match, next, next!, oct, partition, replace, reverse, reverse!, rindex, rjust
| rpartition, rstrip, rstrip!, scan, shellescape, shellsplit, size, slice
| slice!, split, squeeze, squeeze!, start_with?, strip, strip!, sub, sub!, succ
| succ!, sum, swapcase, swapcase!, taguri, taguri=, to_f, to_i, to_s, to_str
| to_sym, to_yaml, tr, tr!, tr_s, tr_s!, unpack, upcase, upcase!, upto
| %, *, +, <<, <=>, ==, =~, [], []=
.
.
v-------------------------------------------------------------------------------
Kernel#
.
.

クラスを引数に取るとシングルトンメソッドを表示

== String.who? ==
String(Class)
| yaml_new, yaml_tag_subclasses?
v-------------------------------------------------------------------------------
.
.
Kernel(Module)
.
.

配列

== [1, 2, 3].who? ==
Array#
| assoc, at, choice, clear, collect, collect!, combination, compact, compact!
| concat, count, cycle, delete, delete_at, delete_if, drop, drop_while, each
.
.

ライブラリもOK

== ActiveRecord::Base.who? ==
ActiveRecord::Base(Class)
| count_by_sql, find_by_sql, sanitize_sql, sanitize_sql_array
| sanitize_sql_for_assignment, sanitize_sql_for_conditions, sanitize_sql_hash
| sanitize_sql_hash_for_assignment, sanitize_sql_hash_for_conditions
v-------------------------------------------------------------------------------
ActiveRecord::Aggregations(Module)
v-------------------------------------------------------------------------------
ActiveRecord::Transactions(Module)
.
.
Kernel(Module)
v-------------------------------------------------------------------------------

詳しい実行結果は こちら

特殊機能

moroさんの改良版に影響を受けtapスタイルになってます。他にも色々な機能を追加しました。

require 'rubygems'
require 'rubywho'

# フィルタリング
1.who?("to_")

# クラス階層の深さを指定
1.who?(nil, 3)

# ショートカット
1.who1?
puts "== who0? display 1-line. =="
1.who0?

# 強制的にインスタンスメソッドを表示
1.who_instance?
Fixnum.who_i?

# 強制的にシングルトンメソッドを表示
1.who_singleton?
Fixnum.who_s?

# メソッドチェインの間に挟み込む
puts "== who0? method chain. =="
[1, "cat", "cat", 2, "cat", 3].who0?.select{|i| i == 'cat'}.who0?.count.who0?

実行結果は こちら

インストール

# Windows
$ gem.bat install rubywho

# OSX / Linux
$ sudo gem install rubywho

ソースコード

バグや、感想ありましたら是非教えて下さい。