Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Proc | source_location

概要

Proc#source_location -> [String, Fixnum] | nil

詳細

Procを定義したソースコードのファイル名と行番号を配列で返却。

サンプルコード
require 'tbpgr_utils'

pr1 = Proc.new {}
pr2 = proc {}
lm = lambda {}

bulk_puts_eval binding, <<-EOS
pr1.source_location
pr2.source_location
lm.source_location
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

pr1.source_location # => ["proc_source_location.rb", 3]
pr2.source_location # => ["proc_source_location.rb", 4]
lm.source_location  # => ["proc_source_location.rb", 5]