Bug #7042
closedString#=~ is not equal to Regexp#=~
Description
i found out that string =~ regexp is not equal to regexp =~ string
when you use named captures:
"6:15 PM" =~ /(?\d+):(?\d+) (?A|P)M/; p hour,minute,pm # works not
/(?\d+):(?\d+) (?A|P)M/ =~ "6:15 AM"; p hour,minute,pm # works
i wonder why is that so ?
Updated by stomar (Marcus Stollsteimer) about 12 years ago
=begin
From the documentation for Regexp:
"When named capture groups are used with a literal regexp on the left-hand side of an expression and the =~ operator, the captured text is also assigned to local variables with corresponding names."
Of course, this does not really answer the question regarding the "why".
Regards,
Marcus
=end
Updated by stomar (Marcus Stollsteimer) about 12 years ago
=begin
...and further down in the Regexp docs:
"This assignment is implemented in the Ruby parser. The parser detects ‘regexp-literal =~ expression’ for the assignment. The regexp must be a literal without interpolation and placed at left hand side."
"The assignment does not occur if the regexp is not a literal."
=end
Updated by mame (Yusuke Endoh) about 12 years ago
- Status changed from Open to Rejected
Because this is a kind of assignment, the value is in the right side and the variable is in the left side... we thought.
This is actually intentional. Closing.
You can open a feature request to add a pattern to regexp assignment, if you wish.
--
Yusuke Endoh mame@tsg.ne.jp