Last active
August 29, 2015 13:56
-
-
Save yoshikischmitz/9254803 to your computer and use it in GitHub Desktop.
Splice random characters into a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# builds an array of lower/upper case alphabet characters | |
# and returns a random character from that | |
def rand_char | |
chars = (('a'..'z').to_a + | |
('A'..'Z').to_a + | |
(0..9).to_a ) | |
chars[rand(chars.length)].to_s | |
end | |
# Takes a string str and returns it with random characters | |
# from rand_char spliced into every other character | |
def splice_rand_chars(str) | |
index = 0 | |
until index >= str.length do | |
str[index] += rand_char | |
index += 2 | |
end | |
str | |
end | |
puts splice_rand_chars("SPLICED") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment