I'm getting the error undefined local variable or method '_1' for main:Object
when I run the following code. Any idea why I'm getting this error? and how can I store the result in a variable.
result="-e hostname=webserver001 -e username=root -e password=testing123"
p result.scan(/\w =\w /)
.map { _1.split("=") }
.to_h
Error:
Traceback (most recent call last):
2: from main.rb:4:in `<main>'
1: from main.rb:4:in `map'
main.rb:4:in `block in <main>': undefined local variable or method `_1' for main:Object (NameError)
exit status 1
CodePudding user response:
You're probably running an older version of ruby - the numbered parameters for blocks have been available in ruby since 2.7 - see https://www.bigbinary.com/blog/ruby-2-7-introduces-numbered-parameters-as-default-block-parameters
You can make it work on older version by using
p result.scan(/\w =\w /)
.map { |s| s.split("=") }
.to_h