Escobar Escobar - 1 year ago 80
Ruby Question

Remove the extra and not allowed characters from the ruby line

There is a line:

. I need to
transform it to
(Only letters and numbers). Then trim it to 13 characters
and paste after 3 symbol the first half nubler of digits, paste after 9 symbol second half nubler of digits. For examle have number
and string
. After have

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

Answer Source

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==" 


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

only characters and digits:

 > transform_string ='^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 


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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download