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

class Pathname

クラスの継承リスト: Pathname < Object < Kernel

要約

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

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

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

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

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

Pathname オブジェクトの生成には、Pathname.new のほかに Kernel#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

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

self <=> other -> bool

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

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

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

absolute? -> bool

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

ascend { |pathname| ... }

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

atime -> Time

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

basename(suffix = "") -> Pathname

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

blockdev? -> bool

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

chardev? -> bool

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

chdir(&block)

このメソッドは obsolete です。 代わりに Dir.chdir を使ってください。

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) と同じです。

chroot

このメソッドは obsolete です。 代わりに Dir.chroot を使ってください。

cleanpath(consider_symlink = false) -> Pathname

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

ctime -> Time

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

unlink
delete

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

descend { |pathname| ... }

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

dir_foreach {|pathname| ... }

このメソッドは obsolete です。 代わりに Pathname#each_entry メソッドを使ってください。

directory? -> bool

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

dirname -> Pathname

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

each_entry {|pathname| ... }

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

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

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

each_line(*args, &block)

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

entries

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

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 {|pathname| ...}

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

fnmatch(pattern, *args) -> bool

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

fnmatch?(pattern, *args) -> bool

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

foreach(*args, &block)

このメソッドは obsolete です。 each_line か each_entry を使ってください。

foreachline(*args, &block)

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

ftype -> String

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

grpowned? -> bool

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

hash -> Fixnum

ハッシュ値を返します。

join(*args) -> Pathname

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

lchmod(mode) -> Integer

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

lchown(owner, group) -> Integer

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

link(old) -> 0

レシーバと引数がシステムコールの引数と逆順に対応していて紛らわしいため、 このメソッドは obsolete です。 代わりに Pathname#make_link を使ってください。

lstat -> File::Stat

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

make_link(old)

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

make_symlink(old)

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

mkdir(*args)

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

mkpath

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(&block)

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)

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

readlink -> Pathname

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

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

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

rmtree

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 オブジェクトを生成し、返します。 cf. String#sub

symlink(old) -> 0

レシーバと引数がシステムコールの引数と逆順に対応していて紛らわしいため、 このメソッドは obsolete です。 代わりに Pathname#make_symlink を使ってください。

symlink? -> bool

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

sysopen(*args)

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

to_s -> String
to_str -> 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) と同じです。

zero? -> bool

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

定数

定義 説明
SEPARATOR_PAT

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

TO_PATH

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

継承したメソッド

=~ __id__ __send__ _dump class clone dclone display enum_for equal? extend freeze frozen? initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_yaml to_yaml_properties to_yaml_style untaint