erb.rb: shadow by keys
* lib/erb.rb (ERB#new_toplevel): shadow already defined local variables by block local variabes, not to overwrite them. [ruby-core:84390] [Bug #14215] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
84e462758d
commit
161f4a511a
13
lib/erb.rb
13
lib/erb.rb
@ -889,7 +889,7 @@ class ERB
|
|||||||
# Render a template on a new toplevel binding with local variables specified
|
# Render a template on a new toplevel binding with local variables specified
|
||||||
# by a Hash object.
|
# by a Hash object.
|
||||||
def result_with_hash(hash)
|
def result_with_hash(hash)
|
||||||
b = new_toplevel
|
b = new_toplevel(hash.keys)
|
||||||
hash.each_pair do |key, value|
|
hash.each_pair do |key, value|
|
||||||
b.local_variable_set(key, value)
|
b.local_variable_set(key, value)
|
||||||
end
|
end
|
||||||
@ -900,8 +900,15 @@ class ERB
|
|||||||
# Returns a new binding each time *near* TOPLEVEL_BINDING for runs that do
|
# Returns a new binding each time *near* TOPLEVEL_BINDING for runs that do
|
||||||
# not specify a binding.
|
# not specify a binding.
|
||||||
|
|
||||||
def new_toplevel
|
def new_toplevel(vars = nil)
|
||||||
TOPLEVEL_BINDING.dup
|
b = TOPLEVEL_BINDING
|
||||||
|
if vars
|
||||||
|
vars = vars.select {|v| b.local_variable_defined?(v)}
|
||||||
|
unless vars.empty?
|
||||||
|
return b.eval("tap {|;#{vars.join(',')}| break binding}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
b.dup
|
||||||
end
|
end
|
||||||
private :new_toplevel
|
private :new_toplevel
|
||||||
|
|
||||||
|
@ -611,6 +611,10 @@ EOS
|
|||||||
erb = @erb.new("<%= foo %>")
|
erb = @erb.new("<%= foo %>")
|
||||||
erb.result_with_hash(foo: "1")
|
erb.result_with_hash(foo: "1")
|
||||||
assert_equal(false, TOPLEVEL_BINDING.local_variable_defined?(:foo))
|
assert_equal(false, TOPLEVEL_BINDING.local_variable_defined?(:foo))
|
||||||
|
TOPLEVEL_BINDING.eval 'template2 = "two"'
|
||||||
|
erb = @erb.new("<%= template2 %>")
|
||||||
|
erb.result_with_hash(template2: "TWO")
|
||||||
|
assert_equal "two", TOPLEVEL_BINDING.local_variable_get("template2")
|
||||||
end
|
end
|
||||||
|
|
||||||
# This depends on the behavior that #local_variable_set raises TypeError by invalid key.
|
# This depends on the behavior that #local_variable_set raises TypeError by invalid key.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user