Actions
Misc #18125
closedA strange behavior when same name variable/method coexist issue.
Status:
Closed
Assignee:
-
Description
Following is a example.
Reproduce¶
def deploy_to
"deploy_to"
end
deploy_to = "#{deploy_to} new place"
p defined? deploy_to # => local_varible
p deploy_to # => " new place"
What we expected.¶
def deploy_to
"deploy_to"
end
deploy_to = "#{deploy_to} new place"
p defined? deploy_to # => local_varible
p deploy_to # => "deploy_to new place"
This strange behavior i found it after several years ruby programming.
Perhaps this is always the expected behavior?
i report here for check anyway
Updated by byroot (Jean Boussier) over 3 years ago
It is excepted because of variable hoisting.
Same reason why foo = 1 unless foo
works.
Updated by matz (Yukihiro Matsumoto) over 3 years ago
- Status changed from Open to Closed
Local variables are defined when they appear in assignments, thus
deploy_to = "#{deploy_to} new place"
# ^ assignment ^local variable
To avoid ambiguity, use parenthesis.
deploy_to = "#{deploy_to()} new place"
Matz.
Actions
Like0
Like0Like0