Learning Ruby Caesar Cipher I am trying to learn Ruby and made a simple Caesar cipher program and I was wondering if the following code follows the 'ruby way' or the way most ruby users would do it 

# Implements the classes caesar cipher encryption and decryption.
class CaesarCipher
  def initialize(shift)
    @shift = shift
  end

  def encrypt(str)
    str_array = str.split("")
    str_array.map! { |char| round_over(char) }.join
  end

  def decrypt(str)
    str_array = str.split("")
    str_array.map! { |char| round_down(char) }.join
  end


  private
  def round_over(char)
    if char.ord + @shift &gt; "z".ord
      (char.ord + @shift - 26).chr
    elsif char.ord + @shift &gt; "Z".ord &amp;&amp; char.ord &lt;= "Z".ord
      (char.ord + @shift - 26).chr
    else
      (char.ord + @shift).chr
    end
  end

  def round_down(char)
    if char.ord - @shift &lt; "a".ord &amp;&amp; /[[:lower:]]/.match(char) # checks for lowercase
      (char.ord - @shift + 26).chr
    elsif char.ord - @shift &lt; "A".ord
      (char.ord - @shift + 26).chr
    else
      (char.ord - @shift).chr
    end
  end
end
That's a pretty good start, however I noticed that your code only works on letters. For non-alphabetic characters it returns confusing results e.g. encrypting and decrypting ! gives you ;. I can think of three good ways to handle non-alphabetic characters:


Ignore them
Use ASCII codes to shift them as well (so Z shifted right one becomes [)
Raise an error


Any of those approaches will ensure that decrypting and encrypting are inverses (which should be a property of any cipher!). That also leads you to a nice way to refactor your code: decrypting with a shift of 5 is the same thing as encrypting with a shift of -5. Try rewriting your code to look like this:

def encrypt(str)
  str.split('').map { |char| round(char, @shift) }.join
end

def decrypt(str)
  str.split('').map { |char| round(char, -@shift) }.join
end

private
def round(char, shift)
  # ...
end


Other than that, just a couple tips:


Ways to compare ranges of characters: /[a-z]/i =~ chr or (?A..?Z) === chr
What if @shift is greater than 26? You can use modulus % to account for shifts of any size