Clear all variables in rails console

can any body tell me which command is used to clear all variables in the rails console?

eg.

1.9.1 :001 > permissions = {:show => true} => {:show=>true} 1.9.1 :001 > foo = "bar" => "bar" 

I need a command that can get all variables reset to zero without rebooting the rails console itself.

Any advice would be greatly appreciated.

+4
source share
2 answers
 local_variables.each { |e| eval("#{e} = nil") } 

local_variables returns a list of characters of all local variables in the current scope

 a, b = 5, 10 local_variables # => [:b, :a] 

Using each , you can iterate over this list with eval to assign their nil values.

You can also do the same with instance_variables and global_variables . for instance

 (local_variables + instance_variables).each { |e| eval("#{e} = nil") } 

By the way, if you intend to use it more than once, it may be useful to define such a method in the ~/.irbrc file to make it available for all irb sessions (did not test it in the rails console).

 class Binding def clear eval %q{ local_variables.each { |e| eval("#{e} = nil") } } end end 

Then inside the irb session

 a = 5 binding.clear a # => nil 
+7
source

Do one enter

 irb 'another' 

and then press ctrl + l

now check the values โ€‹โ€‹of your variables. He works.

-2
source

Source: https://habr.com/ru/post/1448085/


All Articles