Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Last active August 29, 2015 14:00
Show Gist options
  • Save mathildathompson/929e1313deb34eea0cb1 to your computer and use it in GitHub Desktop.
Save mathildathompson/929e1313deb34eea0cb1 to your computer and use it in GitHub Desktop.
# require 'pry'
class Crypto
def initialize(message)
@message = message
end
def cipher_message
plain_text = convert_plain_text
letter_segments = convert_segments(plain_text)
cipher(letter_segments)
end
def convert_plain_text
@message.split('').map(&:downcase).select do |character|
/[a-z]+/ =~ character
end
end
def convert_segments(text)
length = Math.sqrt(text.length).ceil
text.each_slice(length).to_a
end
def cipher(segments)
cipher_hash = {}
segments.each do |segment|
segment.each_with_index do |letter, index|
cipher_hash[index] ||= []
cipher_hash[index] << letter
end
end
puts cipher_hash.values.join('')
end
end
puts Crypto.new('I am a secret message!! Nobody should be able to understand me!').cipher_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment