new to ruby (and coding) vigenere cipher tips

# Any methods or tips to clean this up would be appreciated (Examples @ bottom) :-) def vigenere_cipher(message, keys) alpha = "abcdefghijklmnopqrstuvwxyz" arr = [] vigenere = "" (0..message.length - keys.length).step(keys.length) do |num| arr << message[num..(num + (keys.length - 1))] end if message.length % keys.length != 0 arr << message[-(message.length % keys.length)..-1] end arr.each do |str| str.each_char.with_index do |char, inx| vigenere << alpha[(alpha.index(char) + keys[inx % keys.length]) % 26] end end vigenere end p vigenere_cipher("toerrishuman", [1]) # => "upfssjtivnbo" p vigenere_cipher("toerrishuman", [1, 2]) # => "uqftsktjvobp" p vigenere_cipher("toerrishuman", [1, 2, 3]) # => "uqhstltjxncq" p vigenere_cipher("zebra", [3, 0]) # => "ceerd" p vigenere_cipher("yawn", [5, 1]) # => "dbbo" 

submitted by /u/Flara801
[link] [comments]


Go to Source of this post
Author Of this post: /u/Flara801
Title Of post: new to ruby (and coding) vigenere cipher tips
Author Link: {authorlink}