概要
pry の中でも利用されている method_source gem を試します
インストール
pry を利用している方はインストール不要
$ gem install method_source
サンプル
サンプルコード: Methods#source_location
source_location.rb
require "set"
require "method_source"
class Hoge
# hoge method header
def hoge
# before hoge
"hoge"
# after hoge
end
end
class Child
include MethodSource::ReeSourceLocation
end
# 自ファイルの source_location
print Hoge.instance_method(:hoge).source_location, "\n"
# source_location gem の source_location
print Child.new.method(:source_location).source_location
- 出力確認
# 自ファイルの source_location
["source_location.rb", 6]
# source_location gem の source_location
["/path/to/ruby/gems/2.1.0/gems/method_source-0.8.2/lib/method_source/source_location.rb", 5]%
サンプルコード: Methods#source
, Methods#source.comment
require "set"
require "method_source"
class Hoge
# hoge method header
def hoge
# before hoge
"hoge"
# after hoge
end
end
puts Hoge.instance_method(:hoge).source
puts Hoge.instance_method(:hoge).comment
- 出力
def hoge
# before hoge
"hoge"
# after hoge
end
# hoge method header