List of local variables in Ruby

We can assign a local thread variable to Ruby, for example:

Thread.current[:foo] = 1 Thread.current[:bar] = 2 

But how do we list / list these variables later?

+4
source share
2 answers

You can get the keys using Thread#keys :

 Thread.current[:foo] = 1 Thread.current[:bar] = 2 Thread.current.keys # => [:__recursive_key__, :foo, :bar] 

I don’t know how useful this is because you need to know which keys you are using.

+6
source
  Thread.list.each { |t| p "T #{t.object_id} tvars:#{t.thread_variables.length} numkeys:#{t.keys.length}" } 
0
source

All Articles