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
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
source share