Class: Womanizer
- Inherits:
-
Object
- Object
- Womanizer
- Defined in:
- lib/womanizer.rb
Constant Summary collapse
- VERSION =
'0.1.1'
Class Method Summary collapse
Instance Method Summary collapse
- #decode(encoded) ⇒ Object
- #define! ⇒ Object
- #encode(source) ⇒ Object
-
#initialize(*words) ⇒ Womanizer
constructor
A new instance of Womanizer.
Constructor Details
#initialize(*words) ⇒ Womanizer
Returns a new instance of Womanizer.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/womanizer.rb', line 11 def initialize(*words) words = ['morewoman', 'morewomen'] if words.empty? words = words.map(&:downcase).uniq raise ArgumentError, 'needs words with length of at least 8' if words.any? { |w| w.length < 8 } @enc = [] @dec = {} chars = 0.upto(255).each begin words.each do |word| l = word.length f = "%0#{l}d" 0.upto(2**l) do |i| o = chars.next b = (f % i.to_s(2)).chars.map(&:to_i) t = word.chars.zip(b).map do |c, u| if u.zero? c else c.upcase end end.join @enc[o] = t @dec[t] = o debug { "#{o} => #{t}" } end end rescue StopIteration #done end end |
Class Method Details
Instance Method Details
#decode(encoded) ⇒ Object
50 51 52 53 |
# File 'lib/womanizer.rb', line 50 def decode(encoded) encoded.split(/\s+/).map { |t| @dec.fetch(t) }.pack('C*'). force_encoding(Encoding.default_external) end |
#define! ⇒ Object
55 56 57 58 59 60 |
# File 'lib/womanizer.rb', line 55 def define! @dec.each do |t,c| Kernel.send(:define_method, t) {|r=''| [c].pack('C')+r } end Kernel.send(:alias_method, 'ok', 'eval') end |
#encode(source) ⇒ Object
46 47 48 |
# File 'lib/womanizer.rb', line 46 def encode(source) source.each_byte.map { |b| @enc[b] }.join(' ') end |