Sha256: e4f266e5f5d8ae51fd16c01352d8c72d94ffeec05236b5744dcc38fc57f607e4

Contents?: true

Size: 1.02 KB

Versions: 177

Compression:

Stored size: 1.02 KB

Contents

module BookKeeping
  VERSION = 1
end

class RotationalCipher
  SMALL_LETTERS_RANGE = (97..122)
  BIG_LETTERS_RANGE   = (65..90)
  ROTATION_MODIFIER   = 1

  attr_reader :text

  def initialize(text, key)
    @text      = text
    @shift_key = key
  end

  def self.rotate(text, key)
    new(text, key).rotate
  end

  def rotate
    text.split('').map { |char| shift_char(char) }.join
  end

  private

  def shift_char(char)
    shift_ascii(char.ord).chr
  end

  def shift_key
    @shift_key % 26
  end

  def shift_ascii(char_ascii)
    case char_ascii
    when SMALL_LETTERS_RANGE
      shift_within(char_ascii, SMALL_LETTERS_RANGE.min, SMALL_LETTERS_RANGE.max)
    when BIG_LETTERS_RANGE
      shift_within(char_ascii, BIG_LETTERS_RANGE.min, BIG_LETTERS_RANGE.max)
    else
      char_ascii
    end
  end

  def shift_within(char_ascii, lower_limit, upper_limit)
    shifted_ascii = char_ascii + shift_key

    return shifted_ascii if shifted_ascii <= upper_limit

    lower_limit + (shifted_ascii - upper_limit - ROTATION_MODIFIER)
  end
end

Version data entries

177 entries across 177 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.179 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.178 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.177 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.176 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.175 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.174 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.173 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.172 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.171 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.170 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.169 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.167 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.166 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.165 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.164 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.163 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.162 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.161 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb
trackler-2.2.1.160 tracks/ruby/exercises/rotational-cipher/.meta/solutions/rotational_cipher.rb