Sha256: e5e353b25a21813edd16fc03b6646c48bf27214648e38b12e4b7df850f250fe3
Contents?: true
Size: 1.15 KB
Versions: 1
Compression:
Stored size: 1.15 KB
Contents
module Squarecoder # A Transcoder is a Singleton used to encode and decode strings. # # Examples # # Squarecoder::Transcoder.encode("haveaniceday") # => "hae and via ecy" # Squarecoder::Transcoder.decode("hae and via ecy") # => "haveaniceday" # "haveaniceday".square_encode # => "hae and via ecy" # "hae and via ecy".square_decode # => "haveaniceday" class Transcoder # Encode a String in square code def self.encode a_string, ignore_whitespace = false, ignore_max_length = false if !ignore_max_length && a_string.length > MAX_LEN raise Squarecoder::InputTooLong end if !ignore_whitespace && a_string.include?(' ') raise Squarecoder::NoWhitespace end len = (a_string.length ** (0.5)).ceil arry = a_string.in_character_arrays_of_length(len) if arry.last.length != len (len - arry.last.length).times { arry.last << ' ' } # a tiny bit faster than arry.fill end arry.transpose.collect { |line| line.join }.join(' ').rstrip end # Decode a String from square code def self.decode a_string encode(a_string, true, true).gsub(/ +/, '') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
squarecoder-0.0.2 | lib/squarecoder/transcoder.rb |