[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77719] [Ruby trunk Bug#12862] Regular Expression Named Group Matching does not work with #{} (Similar? to issue #2778)
From:
matthew@...
Date:
2016-10-22 23:21:06 UTC
List:
ruby-core #77719
Issue #12862 has been updated by Matthew Kerwin.
Leo Amigud wrote:
>
> Sorry, not sure what you mean.
>
> As a matter of fact i can use regexp like:
>
> irb(main):013:0> numbers = '\d+'
> => "\\d+"
> irb(main):014:0> /\$#{numbers}.\d+/ =~ "$3.67"
> => 0
> irb(main):015:0>
>
> to match the string, but using #{numbers} inside the named group somehow does not work.
>
> Is that the expected behaviour?
>
> Leo
It's what Hans quoted and said:
"When named capture groups are used **with a literal regexp** *[i.e. not an interpolated regexp]* ... the captured text is also assigned to local variables ..."
`/(?<dollars>#{numbers})/` is interpolated, not literal.
If you want to use interpolation and named captures, you can still get there using #match:
~~~
irb(main):001:0> numbers = '\d+'
=> "\\d+"
irb(main):002:0> md = /\$(?<dollars>(#{numbers})).(?<cents>\d+)/.match "$4.67"
=> #<MatchData "$4.67" dollars:"4" cents:"67">
irb(main):003:0> md['dollars']
=> "4"
irb(main):004:0> md['cents']
=> "67"
~~~
----------------------------------------
Bug #12862: Regular Expression Named Group Matching does not work with #{} (Similar? to issue #2778)
https://bugs.ruby-lang.org/issues/12862#change-61002
* Author: Leo Amigud
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
Loading development environment (Rails 4.2.5)
irb(main):001:0> /\$(?<dollars>\d+)\.(?<cents>\d+)/ =~ "$3.67"
=> 0
irb(main):002:0> dollars
=> "3"
BUT:
irb(main):001:0> numbers = '\d+'
=> "\\d+"
irb(main):002:0> /\$(?<dollars>#{numbers})\.(?<cents>\d+)/ =~ "$3.67"
=> 0
irb(main):003:0> dollars
NameError: undefined local variable or method `dollars' for main:Object
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>