shoen
shoen

Reputation: 11865

Ruby incorrect array management

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

Upvotes: 1

Views: 78

Answers (1)

glenn mcdonald
glenn mcdonald

Reputation: 15488

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...

Upvotes: 3

Related Questions