Guest User

switch to case and split

a guest
Nov 12th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def caesar_cipher(str, shift = 5)
  2.   alpha = ('a'..'z').to_a
  3.   shifted = []
  4.  
  5.   str = str.split('')
  6.   str.each do |e|
  7.     shifted << case e
  8.                when /[a-z]/
  9.                  alpha[alpha.index(e) - shift]
  10.                when /[A-Z]/
  11.                  alpha[alpha.index(e)].upcase
  12.                else
  13.                  e
  14.                end
  15.   end
  16.  
  17.   shifted.join
  18. end
Advertisement
Add Comment
Please, Sign In to add comment