1

I have the following piece of code:

nonce = data.scan(/nonce="(.*)"/)

data is a string ,the matched piece of string is assigend to nonce variable which automatically becomes an Array. Now, if i

puts nonce[0]

I will get my value printed correctly:

51d8852d

but if use:

puts "final string #{md1}:#{nonce[0]}:#{md2}"

the output will be:

df49f55acfd9d21837fd840644f251b4:["51d8852d"]:3b7718806908d2a4456086be7daba94ccd36ea19fd2bfa80ae41fa8be23433b7

but there shouldn't be any brackets or duoble quotes, i should get only the array's value. It should be something like this:

df49f55acfd9d21837fd840644f251b4:51d8852d:3b7718806908d2a4456086be7daba94ccd36ea19fd2bfa80ae41fa8be23433b7

Could you please suggest me how to solve this issue? Thanks

Dawid

1 Answer 1

3

When you use scan with a capture group, the result is an array of arrays, so you want to use nonce[0][0]. You got confused because your first example feeds nonce[0], which is an array, to puts, which handles arrays by printing out each element. If you do puts nonce[0].class, you'll see...

Sign up to request clarification or add additional context in comments.

1 Comment

Or use nonce.flatten[0]. The end result is the same so it really depends on what looks better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.