[ruby-list:50657] Re: [質問] 関数内にて引数に渡された変数の変数名を取得

From: <yamataka@...08.itscom.net>
Date: 2018-03-22 05:09:33 UTC
List: ruby-list #50657
山口です。

> Miyagiです。
>
> かなり無理やりな感じですがTracePointを使ってこういう感じで書けそうです。

こんなやり方があるのですね。
期待していたとおりのものです。
これから、中身理解します。

ありがとうございました。

> 
> music = {
>   ex1: 'Play the top pop station'
> }
> 
> def tbl_say(tbl,ex)
>   unless tbl.has_key?(ex)
>     puts "[ERROR] #{ex} is not in #{$tbl_name}"
>   end
> end
> 
> TracePoint.trace(:line) do |tp|
>   $b = tp.binding
> end
> 
> TracePoint.trace(:call) do |tp|
>   return unless tp.method_id == :tbl_say
>   tbl = tp.binding.local_variable_get(:tbl)
>   $tbl_name = $b.local_variables.detect {|v| $b.local_variable_get(v) 
== tbl }
> end
> 
> tbl_say(music,:ex1)
> tbl_say(music,:ex2)
 



In This Thread

Prev Next