Class: Huff::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/huff/encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ Encoder

Returns a new instance of Encoder.



3
4
5
# File 'lib/huff/encoder.rb', line 3

def initialize(tree)
  @table = EncodingTableBuilder.new(tree).table
end

Instance Method Details

#encode_36(text) ⇒ Object



13
14
15
# File 'lib/huff/encoder.rb', line 13

def encode_36(text)
  encode_string(text).to_i(2).to_s(36)
end

#encode_string(text) ⇒ Object



7
8
9
10
11
# File 'lib/huff/encoder.rb', line 7

def encode_string(text)
  text.each_char.each_with_object('') do |char, result|
    result << @table[char]
  end
end