Actions
Bug #14215
closedresult_with_hash change local variable by hash value.
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0rc1 (2017-12-14 trunk 61243) [x86_64-darwin16]
Backport:
Description
When there is a local variable with the same name as the hash key,
ERB#result_with_hash changes the local variable by hash value.
This is sample code.
require "erb"
erb_obj = ERB.new("<%=name%> \"Hello <%=first_name%> <%=last_name%>!\"")
name = 'honoka'
last_name = 'kousaka'
puts erb_obj.result_with_hash(first_name: 'kotori', last_name: 'minami')
puts "Top level name is #{name}, #{last_name}"
user = {first_name: 'umi', last_name: 'sonoda'}
puts erb_obj.result_with_hash(user)
puts "Top level name is #{name}, #{last_name}"
This code's output is...
honoka "Hello kotori minami!"
Top level name is honoka, minami # (not honoka, kousaka)
honoka "Hello umi sonoda!"
Top level name is honoka, sonoda # (not honoka, kousaka)
I think if changes are made within ERB it will be as intended,
but method argument change local variables is an unexpected behavior for user.
(Hash key and variable are completely different concepts...)
I think that the above code should be as follows output.
honoka "Hello kotori minami!"
Top level name is honoka, kousaka
honoka "Hello umi sonoda!"
Top level name is honoka, kousaka
I write patch it's backup local variable and reset after.
Sorry I don't know how to undefine local valiable so I set nil.
news.each { |key| b.local_variable_set(key, nil) }
But there are side effects... please let me know more good code.
Files
Actions
Like0
Like0