def self.combine_mode_and_option(mode = nil, options = Backports::Undefined)
mode, options = nil, mode if mode.is_a?(Hash) and options == Backports::Undefined
options = {} if options == Backports::Undefined
if mode && options[:mode]
raise ArgumentError, "mode specified twice"
end
mode ||= options[:mode] || "r"
if options[:textmode] || options[:binmode]
text = options[:textmode] || (mode.is_a?(String) && mode =~ /t/)
bin = options[:binmode] || (mode.is_a?(String) ? mode =~ /b/ : mode & File::Constants::BINARY != 0)
if text && bin
raise ArgumentError, "both textmode and binmode specified"
end
case
when !options[:binmode]
when mode.is_a?(String)
mode.insert(1, "b")
else
mode |= File::Constants::BINARY
end
end
mode
end