u16suzuの blog

日々学んだことのメモブログです。

Method#source_locationで メソッドの定義場所を探す

Method#source_location が便利だと @ikm さんに教わったので使ってみた。

method(:method_name).source_location とすると, メソッドを定義しているファイル名と行数を返してくれる。

#hoge.rb
require './fuga'
def mel; end

p method(:mel).source_location # => ["hoge.rb", 147]

# 外部ファイルの場合はこんな感じです。
p method(:hoge33).source_location #=> ["/Users/u16suzu/tmp/fuga.rb", 1]
#fuga.rb
def hoge33; end

いつもソースリーディングのときは git grep していたのですが、これは便利ですね!
gem を読むのも随分早くなりそうです。