Escobar Escobar - 2 months ago 6
Ruby Question

Remove the extra and not allowed characters from the ruby line

There is a line:

ggSuQNs6TxOTuQDd0j+4sA==$QO/Mq2jwfe3jgsGGoIGmlg==
. I need to
transform it to
ggSuQNs6TxOTuQDd0j4sAQOMq2jwfe3jgsGGoIGmlg
(Only letters and numbers). Then trim it to 13 characters
ggSuQNs6TxOTu
and paste after 3 symbol the first half nubler of digits, paste after 9 symbol second half nubler of digits. For examle have number
3243
and string
ggSuQNs6TxOTu
. After have
ggS32uQNs436TxOTu


My english is bad), i hope you understand me.

Answer

Here is my try as what I could understand from your question (Let me go through with your each sentences).

Your string:

s = "ggSuQNs6TxOTuQDd0j+4sA==$QO/Mq2jwfe3jgsGGoIGmlg==" 

Step-1

I need to transform it to "ggSuQNs6TxOTuQDd0j4sAQOMq2jwfe3jgsGGoIGmlg" (Only letters and numbers).

only characters and digits:

 > transform_string = s.tr('^A-Za-z0-9', '')
 #=> "ggSuQNs6TxOTuQDd0j4sAQOMq2jwfe3jgsGGoIGmlg" 

Step -2

Then trim it to 13 characters "ggSuQNs6TxOTu"

fetch first 13 characters like this way:

 > thirteen_chrs = transform_string[0..12]
 #=> "ggSuQNs6TxOTu" 

Step-3 (from your example)

number = 3242 

Step-4

after 3 characters the first half number of digits, paste after 9 characters second half number of digits.

here is in-line code for same:

> thirteen_chrs.insert(3, number.to_s.chars.each_slice(2).map(&:join).first).insert(9, number.to_s.chars.each_slice(2).map(&:join).last)
#=> "ggS3232uQ42Ns6TxOTu" 

I hope this may help you :)