Sha256: e9a506aa4323d60a8c002356bfa6578d66f4d6abfc5550794d8e8b8636441a8d

Contents?: true

Size: 745 Bytes

Versions: 3

Compression:

Stored size: 745 Bytes

Contents

# frozen_string_literal: true

require 'ruby-fire'

def caesar_encode(n = 0, text = '')
  text.chars.map { |char| _caesar_shift_char(n, char) } * ''
end

def caesar_decode(n = 0, text = '')
  caesar_encode(-n, text)
end

def rot13(text)
  caesar_encode(13, text)
end

def _caesar_shift_char(n = 0, char = ' ')
  return char unless /^[A-Za-z]$/ =~ char
  return ((char.ord - 'A'.ord + n) % 26 + 'A'.ord).chr if /^[A-Z]$/ =~ char

  ((char.ord - 'a'.ord + n) % 26 + 'a'.ord).chr
end

# puts rot13('Hello world!') == 'Uryyb jbeyq!'
# puts rot13('Uryyb jbeyq!') == 'Hello world!'
# puts caesar_encode(1, 'Hello world!') == 'Ifmmp xpsme!'
# puts caesar_decode(1, 'Ifmmp xpsme!') == 'Hello world!'

Fire.fire(program_name: 'cipher') if __FILE__ == $0

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-fire-0.2.2 examples/cipher.rb
ruby-fire-0.2.1 examples/cipher.rb
ruby-fire-0.1.0 examples/cipher.rb