mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
use Binding#local_variable_set when available
ruby2.1+ or somewhere near there added Binding#local_variable_set. pry uses it when it's available because it lets pry avoid setting a temporary thread-local variable for the current pry instance.
This commit is contained in:
parent
f6873cd9f5
commit
b23e9256ac
1 changed files with 25 additions and 8 deletions
|
@ -132,16 +132,33 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Injects a local variable into the provided binding.
|
||||
# @param [String] name The name of the local to inject.
|
||||
# @param [Object] value The value to set the local to.
|
||||
# @param [Binding] b The binding to set the local on.
|
||||
# @return [Object] The value the local was set to.
|
||||
#
|
||||
# @param [String] name
|
||||
# The name of the local to inject.
|
||||
#
|
||||
# @param [Object] value
|
||||
# The value to set the local to.
|
||||
#
|
||||
# @param [Binding] b
|
||||
# The binding to set the local on.
|
||||
#
|
||||
# @return [Object]
|
||||
# The value the local was set to.
|
||||
#
|
||||
def inject_local(name, value, b)
|
||||
Pry.current[:pry_local] = value.is_a?(Proc) ? value.call : value
|
||||
b.eval("#{name} = ::Pry.current[:pry_local]")
|
||||
ensure
|
||||
Pry.current[:pry_local] = nil
|
||||
value = value.is_a?(Proc) ? value.call : value
|
||||
if b.respond_to?(:local_variable_set)
|
||||
b.local_variable_set name, value
|
||||
else # < 2.1
|
||||
begin
|
||||
Pry.current[:pry_local] = value
|
||||
b.eval "#{name} = ::Pry.current[:pry_local]"
|
||||
ensure
|
||||
Pry.current[:pry_local] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# @return [Integer] The maximum amount of objects remembered by the inp and
|
||||
|
|
Loading…
Reference in a new issue