class Object

Public Instance Methods

display_in_browser(html_source, path=nil) click to toggle source

Write HTML source to a file, and launch browser on that file.

# File huffman.rb, line 61
def display_in_browser(html_source, path=nil)
    path = "#{Dir.pwd}/tmpHuffman.html" if path == nil
    url = "file:///#{path}"
    warn "Launch browser on URL #{url}"
    begin
        html = File.new(path, 'w+')
    rescue
        warn "Cannot open #{path} to write HTML."
    else
        begin
            html << html_source
            begin
                Launchy.open(url)
            rescue
                warn "Launch failed. Try browsing URL manually."
            end
        ensure
            html.close
        end
    end
end
run(argument) click to toggle source

TO DO: document

# File huffman.rb, line 44
def run(argument)
    io = HuffmanIO.new(argument)
    begin
        source = Source.new(io.symbols)
        codec = HuffmanCodec.new(source.frequencies)
        codec.encode(io.symbols, io.encoded)
        codec.decode(io.encoded, io.decoded)
        io.report(source.frequencies.size, source.entropy, codec.cross_entropy)
        display_in_browser(codec.html)
    ensure
        io.close
    end
end
run_as_script() click to toggle source

Process the command line, and pass argument to `run'.

# File huffman.rb, line 30
def run_as_script
    begin
        if ARGV.length != 1 then
            raise UserError, "USAGE #$0 [--string=<string> | <path>]"
        end
        run ARGV[0]
    rescue UserError => e
        warn e.message
        exit -1
    end
end
warn(message) click to toggle source

Write script name along with `message' to standard error.

# File huffman.rb, line 20
def warn(message)
    STDERR.puts "#$0: #{message}"
end