Skip to content

Instantly share code, notes, and snippets.

@yoshikischmitz
Last active August 29, 2015 13:56
Show Gist options
  • Save yoshikischmitz/9254803 to your computer and use it in GitHub Desktop.
Save yoshikischmitz/9254803 to your computer and use it in GitHub Desktop.
Splice random characters into a string
# 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