Sha256: 4775f7c6638669b01cd250c1aa12726c710dce6639ac87123cc745aa50739f28
Contents?: true
Size: 1.56 KB
Versions: 10
Compression:
Stored size: 1.56 KB
Contents
# frozen_string_literal: true class String # "Encrypt / Decrypt" the string with Caesar cipher. This will shift the # alphabet letters by +n+, where +n+ is the integer key. The same function # is used for encryption / decryption. # @param opts [Hash] optional parameters # @option opts [Integer] :shift The shift key. Default value: 13. # @see https://en.wikipedia.org/wiki/Caesar_cipher # @return [String] the (de)ciphered string # @example # 'Hello world!'.rot # => "Uryyb jbeyq!" # 'Hello world!'.rot(shift: 11) # => "Spwwz hzcwo!" # 'Uryyb jbeyq!'.rot # => "Hello world!" # 'Spwwz hzcwo!'.rot(shift: 26-11) # => "Hello world!" def rot(opts = {}) opts[:shift] ||= 13 alphabet = Array('a'..'z') lowercase = Hash[alphabet.zip(alphabet.rotate(opts[:shift]))] alphabet = Array('A'..'Z') uppercasecase = Hash[alphabet.zip(alphabet.rotate(opts[:shift]))] encrypter = lowercase.merge(uppercasecase) chars.map { |c| encrypter.fetch(c, c) }.join end # "Encrypt / Decrypt" the string with Caesar cipher in place as described for # {String#rot}. # @return [String] the (de)ciphered string as well as changing changing the # object in place. # @example # a = 'Bonjour le monde !' # => "Bonjour le monde !" # a.rot! # => "Obawbhe yr zbaqr !" # a # => "Obawbhe yr zbaqr !" def rot!(opts = {}) replace(rot(opts)) end # Alias for {String#rot} with default value ( +rot(shift: 13)+ ). def rot13 rot end # Alias for {String#rot!} with default value ( +rot!(shift: 13)+ ). def rot13! rot! end end
Version data entries
10 entries across 10 versions & 1 rubygems