Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/file.rb

Overview

Core-Extensions on File

Constant Summary collapse

POSIX_ABSOLUTE_PATH_PATTERN =
/\A\//.freeze
WINDOWS_ABSOLUTE_PATH_PATTERN =
Regexp.union(
  POSIX_ABSOLUTE_PATH_PATTERN,
  /\A([A-Z]:)?(\\|\/)/i
).freeze
ABSOLUTE_PATH_PATTERN =
begin
  File::ALT_SEPARATOR ?
    WINDOWS_ABSOLUTE_PATH_PATTERN :
    POSIX_ABSOLUTE_PATH_PATTERN
end

Class Method Summary collapse

Class Method Details

.absolute_path?(path, platform = :default) ⇒ Boolean

determine whether a String path is absolute.

Examples:

File.absolute_path?('foo') #=> false
File.absolute_path?('/foo') #=> true
File.absolute_path?('foo/bar') #=> false
File.absolute_path?('/foo/bar') #=> true
File.absolute_path?('C:foo/bar') #=> false
File.absolute_path?('C:/foo/bar') #=> true

Parameters:

  • path (String)
    • a pathname

Returns:

  • (Boolean)

15
16
17
18
19
20
21
22
23
24
# File 'lib/core_ext/file.rb', line 15

def self.absolute_path?(path, platform = :default)
  pattern = case platform
            when :default then ABSOLUTE_PATH_PATTERN
            when :windows then WINDOWS_ABSOLUTE_PATH_PATTERN
            when :posix   then POSIX_ABSOLUTE_PATH_PATTERN
            else raise ArgumentError, "Unsupported platform '#{platform.inspect}'"
            end

  false | path[pattern]
end