# File lib/htmlentities.rb, line 238
    def encode_entities(*instructions)
        str = nil
        if (instructions.empty?)
            instructions = [:basic] 
        else
            instructions.each do |instr|
                unless ENCODE_ENTITIES_COMMAND_ORDER[instr]
                    raise RuntimeError, "unknown encode_entities command `#{instr.inspect}'"
                end
            end
            instructions.sort! { |a,b|
                ENCODE_ENTITIES_COMMAND_ORDER[a] <=>
                ENCODE_ENTITIES_COMMAND_ORDER[b]
            }
        end
        instructions.each do |instruction|
            case instruction
            when :basic
                # Handled as basic ASCII
                str = (str || self).gsub(HTMLEntities::BASIC_ENTITY_REGEXP) {
                    # It's safe to use the simpler [0] here because we know
                    # that the basic entities are ASCII.
                    '&' << HTMLEntities::REVERSE_MAP[$&[0]] << ';'
                }
            when :named
                # Test everything except printable ASCII 
                str = (str || self).gsub(HTMLEntities::UTF8_NON_ASCII_REGEXP) {
                    cp = $&.unpack('U')[0]
                    (e = HTMLEntities::REVERSE_MAP[cp]) ?  "&#{e};" : $&
                }
            when :decimal
                str = (str || self).gsub(HTMLEntities::UTF8_NON_ASCII_REGEXP) {
                    "&##{$&.unpack('U')[0]};"
                }
            when :hexadecimal
                str = (str || self).gsub(HTMLEntities::UTF8_NON_ASCII_REGEXP) {
                    "&#x#{$&.unpack('U')[0].to_s(16)};"
                }
            end 
        end
        return str
    end