Project

General

Profile

Actions

Bug #7042

closed

String#=~ is not equal to Regexp#=~

Added by Hanmac (Hans Mackowiak) about 12 years ago. Updated about 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
Backport:
[ruby-core:47628]

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0