Olivier Lance wrote in post #1018967:
Hi,
I've just spent some time on this problem until I found the solution,
and I wanted to share it/ask for opinions on this:
I've got a string on which I try to match a regexp. If it matches, I
split the string with one of the captured groups, zip the resulting
array with an array of symbols and merge with a last symbol/string array
pair representing the captured separator.
Here's some code to give you an idea:
n = "a:c"
if n =~ /([a-z])([.:])([a-z])/
p [:first, :second].zip(n.split($2)) | [[:separator, $2]]
end
(see pastie here: http://pastie.org/2445983\)
Do you actually consider that a lucid example of the problem? Bejeesus.
The problem is, the resulting array will contain "nil" for :separator.
Apparently, String#split will reset all regexp-related variables... I
guess it uses regexp internally, but I haven't seen anything documenting
this behavior, and I honestly believed this code would work...
So what's your take on this? Does it feel natural/logical to you?
Should I file a bug to enhance Ruby's documentation?
$ cat ruby.rb
str = 'abcd'
if str =~ /(c)(d)/
p [$1, $2]
str.split('b')
p [$1, $2]
end
$ multiruby ruby.rb
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/ZenTest-4.5.0/lib/multiruby.rb:330:
warning: shadowing outer local variable - s
/Users/me/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/ZenTest-4.5.0/lib/multiruby.rb:391:
warning: shadowing outer local variable - s
VERSION = 1.8.6-p420
CMD = ~/.multiruby/install/1.8.6-p420/bin/ruby ruby.rb
["c", "d"]
[nil, nil]
RESULT = pid 3744 exit 0
VERSION = 1.8.7-p352
CMD = ~/.multiruby/install/1.8.7-p352/bin/ruby ruby.rb
["c", "d"]
[nil, nil]
RESULT = pid 3745 exit 0
VERSION = 1.9.1-p431
CMD = ~/.multiruby/install/1.9.1-p431/bin/ruby ruby.rb
["c", "d"]
["c", "d"]
RESULT = pid 3746 exit 0
VERSION = 1.9.2-p290
CMD = ~/.multiruby/install/1.9.2-p290/bin/ruby ruby.rb
["c", "d"]
["c", "d"]
RESULT = pid 3747 exit 0
TOTAL RESULT = 0 failures out of 4
Passed: 1.8.6-p420, 1.8.7-p352, 1.9.1-p431, 1.9.2-p290
Failed:
$
···
--
Posted via http://www.ruby-forum.com/\.