class Object

Public Class Methods

open(*args) { |f| ... } click to toggle source
# File lib/batch-kit/core_ext/file.rb, line 35
def open(*args, &blk)
    if args.length >= 2 && (mode_string = args[1]).is_a?(String) &&
        mode_string =~ /^(w|a):(.*)bom/i
        write_mode = $1.downcase == 'w'
        args[1] = mode_string.sub(/bom\||[\-|]bom/, '')
        f = open_without_bom(*args)
        bom_hex = case mode_string
        when /utf-?8/i
            "\xEF\xBB\xBF"
        when /utf-16be/i
            "\xFE\xFF"
        when /utf-16le/i
            "\xFF\xFE"
        when /utf-32be/i
            "\x00\x00\xFE\xFF"
        when /utf-32le/i
            "\xFE\xFF\x00\x00"
        end
        f << bom_hex.force_encoding(f.external_encoding) if write_mode
        if block_given?
            yield f
            f.close
        else
            f
        end
    else
        open_without_bom(*args, &blk)
    end
end