Sha256: 82cd5ca238d80c5d2e882672ed07bc7b1be371a0c01b2a8464fae42f15465936

Contents?: true

Size: 494 Bytes

Versions: 396

Compression:

Stored size: 494 Bytes

Contents

class Atbash
  def self.encode(plaintext)
    new(plaintext).encode
  end

  attr_reader :plaintext

  def initialize(plaintext)
    @plaintext = plaintext
  end

  def encode
    chunk convert(normalize(plaintext))
  end

  private

  def convert(s)
    s.tr(alphabet, key)
  end

  def chunk(s)
    s.scan(/.{1,5}/).join(' ')
  end

  def normalize(s)
    s.downcase.gsub(/[^a-z0-9]/, '')
  end

  def alphabet
    'abcdefghijklmnopqrstuvwxyz'
  end

  def key
    alphabet.reverse
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

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