Actions
Feature #18291
openWhen use =~ with named group, if regex is on the right side, variable not defined.
Status:
Open
Assignee:
-
Target version:
-
Description
Following code not work.
if "Billy Zheng" =~ /(?<first_name>\w+)\s+(?<last_name>\w+)/
p first_name
p last_name
end
# NameError: undefined local variable or method `first_name' for main:Object
But, if we switch left and right between =~
if /(?<first_name>\w+)\s+(?<last_name>\w+)/ =~ "Billy Zheng"
p first_name
p last_name
end
# => "Billy"
# =>"Zheng"
I know ruby keep this same behavior since 1.9, but i am curious if we can improve this?
I consider this as a bug, because that not good, when i want to use this way, i have to
take care must write regexp before =~
Thank you.
Actions
Like0
Like0Like0