Ruby 2.3.0 リファレンスマニュアル > ライブラリ一覧 > pathnameライブラリ > Pathnameクラス

class Pathname

クラス・モジュールの継承リスト: Pathname < Object < Kernel < BasicObject

要約

パス名をオブジェクト指向らしく扱うクラスです。

Pathname オブジェクトはパス名を表しており、ファイルやディレクトリそのものを表してはいません。当然、存在しないファイルのパス名も扱えます。

絶対パスも相対パスも扱えます。

Pathname オブジェクトは immutable であり、自身を破壊的に操作するメソッドはありません。

Pathname のインスタンスメソッドには、ディレクトリのパスを返す Pathname#dirname のように、文字列操作だけで結果を返すものもあれば、ファイルの中身を読み出す Pathname#read のようにファイルシステムにアクセスするものもあります。

Pathname オブジェクトの生成には、Pathname.new のほかに Kernel#Pathname も使えます。



require 'pathname'

Pathname.new("foo/bar") # => #<Pathname:foo/bar>
Pathname("foo/bar")     # => #<Pathname:foo/bar>

特異メソッド

定義 説明
getwd -> Pathname
pwd -> Pathname

カレントディレクトリを元に Pathname オブジェクトを生成します。 Pathname.new(Dir.getwd) と同じです。

glob(pattern, flags=0) -> [Pathname]
glob(pattern, flags=0) {|pathname| ...} -> nil

ワイルドカードの展開を行なった結果を、 Pathname オブジェクトの配列として返します。

new(path) -> Pathname

文字列 path を元に Pathname オブジェクトを生成します。

インスタンスメソッド

定義 説明
self + other -> Pathname
self / other -> Pathname

パス名を連結します。つまり、other を self からの相対パスとした新しい Pathname オブジェクトを生成して返します。

self <=> other -> -1 | 0 | 1 | nil

パス名を比較します。other と同じなら 0 を、ASCII順で self が大きい場合は正、other が大きい場合は負を返します。大文字小文字は区別されます。 other は Pathname オブジェクトでなければなりません。

self == other -> bool
self === other -> bool
eql?(other) -> bool

パス名を比較し、 other と同じなら真を返します。大文字小文字は区別されます。 other は Pathname オブジェクトでなければなりません。

absolute? -> bool

self が絶対パス指定であれば真を返します。

ascend {|pathname| ... } -> nil
ascend -> Enumerator

self のパス名から親方向に辿っていったときの各パス名を新しい Pathname オブジェクトとして生成し、ブロックへの引数として渡して実行します。ブロックを省略した場合は Enumerator を返します。

atime -> Time

File.atime(self.to_s) を渡したものと同じです。

basename(suffix = "") -> Pathname

Pathname.new(File.basename(self.to_s, suffix)) と同じです。

binread(*args) -> String | nil

IO.binread(self.to_s, *args)と同じです。

binwrite(string, offset=nil) -> Integer

IO.binwrite(self.to_s, *args)と同じです。

birthtime -> Time

File.birthtime(self.to_s) を渡したものと同じです。

blockdev? -> bool

FileTest.blockdev?(self.to_s) と同じです。

chardev? -> bool

FileTest.chardev?(self.to_s) と同じです。

children(with_directory = true) -> [Pathname]

self 配下にあるパス名(Pathnameオブジェクト)の配列を返します。

chmod(mode) -> Integer

File.chmod(mode, self.to_s) と同じです。

chown(owner, group) -> Integer

File.chown(owner, group, self.to_s) と同じです。

cleanpath(consider_symlink = false) -> Pathname

余計な "."、".." や "/" を取り除いた新しい Pathname オブジェクトを返します。

ctime -> Time

File.ctime(self.to_s) を渡したものと同じです。

unlink -> Integer
delete -> Integer

self が指すディレクトリあるいはファイルを削除します。

descend {|pathname| ... } -> nil
descend -> Enumerator

self のパス名の親から子供へと辿っていったときの各パス名を新しい Pathname オブジェクトとして生成し、ブロックへの引数として渡して実行します。ブロックを省略した場合は Enumerator を返します。

directory? -> bool

FileTest.directory?(self.to_s) と同じです。

dirname -> Pathname

Pathname.new(File.dirname(self.to_s)) と同じです。

each_child(with_directory = true) -> Enumerator
each_child(with_directory = true) {|pathname| ...} -> [Pathname]

self.children(with_directory).each と同じです。

each_entry {|pathname| ... } -> nil

Dir.foreach(self.to_s) {|f| yield Pathname.new(f) } と同じです。

each_filename {|v| ... } -> nil

self のパス名要素毎にブロックを実行します。

each_line(*args) {|line| ... } -> nil
each_line(*args) -> Enumerator

IO.foreach(self.to_s, *args, &block) と同じです。

entries -> [Pathname]

self に含まれるファイルエントリ名を元にした Pathname オブジェクトの配列を返します。

executable? -> bool

FileTest.executable?(self.to_s) と同じです。

executable_real? -> bool

FileTest.executable_real?(self.to_s) と同じです。

exist? -> bool

FileTest.exist?(self.to_s) と同じです。

expand_path(default_dir = '.') -> Pathname

Pathname.new(File.expand_path(self.to_s, *args)) と同じです。

extname -> String

File.extname(self.to_s) と同じです。

file? -> bool

FileTest.file?(self.to_s) と同じです。

find(ignore_error: true) -> Enumerator
find(ignore_error: true) {|pathname| ...} -> nil

self 配下のすべてのファイルやディレクトリを一つずつ引数 pathname に渡してブロックを実行します。

fnmatch(pattern, *args) -> bool

File.fnmatch(pattern, self.to_s, *args) と同じです。

fnmatch?(pattern, *args) -> bool

File.fnmatch?(pattern, self.to_s, *args) と同じです。

ftype -> String

File.ftype(self.to_s) と同じです。

grpowned? -> bool

FileTest.grpowned?(self.to_s) と同じです。

hash -> Integer

ハッシュ値を返します。

join(*args) -> Pathname

与えられたパス名を連結します。

lchmod(mode) -> Integer

File.lchmod(mode, self.to_s) と同じです。

lchown(owner, group) -> Integer

File.lchown(owner, group, self.to_s) と同じです。

lstat -> File::Stat

File.lstat(self.to_s) と同じです。

make_link(old) -> 0

File.link(old, self.to_s) と同じです。

make_symlink(old) -> 0

File.symlink(old, self.to_s) と同じです。

mkdir(*args) -> 0

Dir.mkdir(self.to_s, *args) と同じです。

mkpath -> nil

FileUtils.mkpath(self.to_s) と同じです。

mountpoint? -> bool

self がマウントポイントであれば真を返します。

mtime -> Time

File.mtime(self.to_s) を渡したものと同じです。

open(mode = 'r', perm = 0666) -> File
open(mode = 'r', perm = 0666) {|file| ... } -> object

File.open(self.to_s, *args, &block) と同じです。

opendir -> Dir
opendir {|dir| ... } -> nil

Dir.open(self.to_s, &block) と同じです。

owned? -> bool

FileTest.owned?(self.to_s) と同じです。

parent -> Pathname

self の親ディレクトリを指す新しい Pathname オブジェクトを返します。

pipe? -> bool

FileTest.pipe?(self.to_s) と同じです。

read(*args) -> String | nil

IO.read(self.to_s, *args)と同じです。

readable? -> bool

FileTest.readable?(self.to_s) と同じです。

readable_real? -> bool

FileTest.readable_real?(self.to_s) と同じです。

readlines(*args) -> [String]

IO.readlines(self.to_s, *args)と同じです。

readlink -> Pathname

Pathname.new(File.readlink(self.to_s)) と同じです。

realdirpath(basedir = nil) -> Pathname

Pathname#realpath とほぼ同じで、最後のコンポーネントは実際に存在しなくてもエラーになりません。

realpath(basedir = nil) -> Pathname
realpath -> Pathname

余計な "."、".." や "/" を取り除いた新しい Pathname オブジェクトを返します。

relative? -> bool

self が相対パス指定であれば真を返します。

relative_path_from(base_directory) -> Pathname

base_directory から self への相対パスを求め、その内容の新しい Pathname オブジェクトを生成して返します。

rename(to) -> 0

File.rename(self.to_s, to) と同じです。

rmdir -> 0

Dir.rmdir(self.to_s) と同じです。

rmtree -> nil

FileUtils.rm_r(self.to_s) と同じです。

root? -> bool

self がルートディレクトリであれば真を返します。判断は文字列操作によって行われ、ファイルシステムはアクセスされません。

setgid? -> bool

FileTest.setgid?(self.to_s) と同じです。

setuid? -> bool

FileTest.setuid?(self.to_s) と同じです。

size -> Integer

FileTest.size(self.to_s) と同じです。

size? -> bool

FileTest.size?(self.to_s) と同じです。

socket? -> bool

FileTest.socket?(self.to_s) と同じです。

split -> Array

File.split(self.to_s) と同じです。

stat -> File::Stat

File.stat(self.to_s) と同じです。

sticky? -> bool

FileTest.sticky?(self.to_s) と同じです。

sub(pattern, replace) -> Pathname
sub(pattern) {|matched| ... } -> Pathname

self を表現するパス文字列に対して sub メソッドを呼び出し、その結果を内容とする新しい Pathname オブジェクトを生成し、返します。

sub_ext(replace) -> Pathname

拡張子を与えられた文字列で置き換えた Pathname オブジェクトを返します。

symlink? -> bool

FileTest.symlink?(self.to_s) と同じです。

sysopen(*args) -> Integer

IO.sysopen(self.to_s, *args)と同じです。

to_path -> String

File.open などの引数に渡す際に呼ばれるメソッドです。 Pathname オブジェクトにおいては、 to_s と同じです。

to_s -> String

パス名を文字列で返します。

truncate(length) -> 0

File.truncate(self.to_s, length) と同じです。

utime(atime, mtime) -> Integer

File.utime(atime, mtime, self.to_s) と同じです。

world_readable? -> bool

FileTest.world_readable?(self.to_s) と同じです。

world_writable? -> bool

FileTest.world_writable?(self.to_s) と同じです。

writable? -> bool

FileTest.writable?(self.to_s) と同じです。

writable_real? -> bool

FileTest.writable_real?(self.to_s) と同じです。

write(string, offset=nil, **opts) -> Integer
zero? -> bool

FileTest.zero?(self.to_s) と同じです。

定数

定義 説明
SEPARATOR_PAT -> Regexp

パス名のなかのディレクトリを区切る部分にマッチする正規表現です。

TO_PATH -> Symbol

内部的に使っている定数です。利用者が使うことはありません。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined !~ =~ _dump class clone define_singleton_method display enum_for equal? extend freeze frozen? initialize initialize_copy inspect instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_str trust untaint untrust untrusted? .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE