def self.combine_mode_and_option(mode = nil, options = Backports::Undefined)
mode, options = nil, mode if mode.respond_to?(:to_hash) && options == Backports::Undefined
options = {} if options == nil || options == Backports::Undefined
options = coerce_to_hash(options)
if mode && options[:mode]
raise ArgumentError, "mode specified twice"
end
mode ||= options[:mode] || "r"
mode = try_convert(mode, String, :to_str) || try_convert(mode, Integer, :to_int) || mode
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