Module: RIO::IF::FileOrDir
- Included in:
- Rio
- Defined in:
- lib/rio/if/fileordir.rb
Instance Method Summary collapse
-
#open(m, *args, &block) ⇒ Object
undocumented.
-
#pos ⇒ Object
For directories calls Dir#pos, otherwise calls IO#pos.
-
#pos=(integer) ⇒ Object
For directories calls Dir#pos=, otherwise calls IO#pos=.
-
#read(*args) ⇒ Object
For directories calls Dir#read, otherwise calls IO#read.
-
#readlink(*args) ⇒ Object
Calls File#readlink.
-
#rename(*args, &block) ⇒ Object
If called with an argument calls FileUtils#rename.
-
#rename!(*args, &block) ⇒ Object
Behaves like IF::FileOrDir#rename, but also changes the calling Rio to refer to the renamed path.
-
#reopen(mode = nil) ⇒ Object
For Streams calls IO#reopen, otherwise closes and re-opens the Rio.
-
#rewind(&block) ⇒ Object
For directories proxies Dir#rewind, otherwise proxies IO#rewind.
-
#seek(*args) ⇒ Object
For directories calls Dir#seek, otherwise calls IO#seek.
-
#symlink(dest) ⇒ Object
Creates a symbolic link dest which points to the Rio’s IF::Path#fspath.
-
#tell ⇒ Object
See IF::FileOrDir#pos.
Instance Method Details
#open(m, *args, &block) ⇒ Object
undocumented
42 |
# File 'lib/rio/if/fileordir.rb', line 42 def open(m,*args,&block) target.open(m,*args,&block); self end |
#pos ⇒ Object
For directories calls Dir#pos, otherwise calls IO#pos
For streams calls IO#pos
ario.pos => integer
ario.tell => integer
Returns the current offset (in bytes) of ario.
f = rio("testfile")
f.pos #=> 0
f.gets #=> "This is line one\n"
f.pos #=> 17
For directories calls Dir#pos
ario.pos => integer
ario.tell => integer
Returns the current position in dir. See also IF::FileOrDir#seek.
d = rio("testdir")
d.pos #=> 0
d.read #=> rio(".")
d.pos #=> 12
227 |
# File 'lib/rio/if/fileordir.rb', line 227 def pos() target.pos end |
#pos=(integer) ⇒ Object
For directories calls Dir#pos=, otherwise calls IO#pos=
For streams calls IO#pos=
ario.pos = integer => 0
Seeks to the given position (in bytes) in ario.
f = rio("testfile")
f.pos = 17
f.gets #=> "This is line two\n"
For directories calls Dir#pos=
ario.pos = integer => integer
Synonym for IF::FileOrDir#seek, but returns the position parameter.
d = rio("testdir") #=> d
d.read #=> rio(".")
i = d.pos #=> 12
d.read #=> rio("..")
d.pos = i #=> 12
d.read #=> rio("..")
254 |
# File 'lib/rio/if/fileordir.rb', line 254 def pos=(integer) target.pos = integer end |
#read(*args) ⇒ Object
For directories calls Dir#read, otherwise calls IO#read
For streams calls IO#read
ario.read([integer [, buffer]]) => string, buffer, or nil
Reads at most integer bytes from the I/O stream, or to the end of file if integer is omitted or is nil
. If the optional buffer argument is present, it must reference a String, which will receive the data. Returns nil
if called at end of file.
f = rio("testfile")
f.read(16) #=> "This is line one"
rio("testfile").read(16) #=> "This is line one"
For directories calls Dir#read
dir.read => ario or nil
Reads the next entry from dir and returns it as a Rio. Returns nil
at the end of the stream.
d = rio("testdir")
d.read #=> rio(".")
d.read #=> rio("..")
d.read #=> rio("config.h")
141 |
# File 'lib/rio/if/fileordir.rb', line 141 def read(*args) target.read(*args)end |
#readlink(*args) ⇒ Object
Calls File#readlink
Returns a Rio referencing the file referenced by the given link. Not available on all platforms.
82 |
# File 'lib/rio/if/fileordir.rb', line 82 def readlink(*args) target.readlink(*args) end |
#rename(*args, &block) ⇒ Object
If called with an argument calls FileUtils#rename. If called without an argument puts the Rio in “rename mode”.
Proxy for FileUtils#rename
ario = rio('afile.cpp')
ario.rename('afile.cxx') # renamed the file, but ario still references
# the old path
Rename Mode
In rename mode, changes to a Rio’s path using IF::Path#dirname=, IF::Path#filename=, IF::Path#basename=, and IF::Path#extname= also cause the object on the filesystem to be renamed.
Change the extension of all’.cpp’ files in ‘adir’ to ‘.cxx’
rio('adir').rename.files('*.cpp') do |file|
file.ext = '.cxx' # 'file' references the new path and the actual file is renamed
end
Recursively change all ‘.tar.gz’ files to ‘.tgz’ files
rio('adir').rename.all.files('*.tar.gz') do |gzfile|
gzfile.ext('.tar.gz').ext = '.tgz'
end
See IF::Path#dirname=, IF::Path#filename=, IF::Path#basename=, and IF::Path#extname=
109 |
# File 'lib/rio/if/fileordir.rb', line 109 def rename(*args,&block) target.rename(*args,&block); self end |
#rename!(*args, &block) ⇒ Object
Behaves like IF::FileOrDir#rename, but also changes the calling Rio to refer to the renamed path
114 |
# File 'lib/rio/if/fileordir.rb', line 114 def rename!(*args,&block) target.rename!(*args,&block); self end |
#reopen(mode = nil) ⇒ Object
For Streams calls IO#reopen, otherwise closes and re-opens the Rio.
259 |
# File 'lib/rio/if/fileordir.rb', line 259 def reopen(mode=nil) target.reopen(mode); self end |
#rewind(&block) ⇒ Object
For directories proxies Dir#rewind, otherwise proxies IO#rewind
Proxy for IO#rewind
ario.rewind => ario
Positions ario to the beginning of input, resetting lineno to zero.
Returns the Rio
f = rio("testfile")
f.readline #=> "This is line one\n"
f.rewind #=> f
f.lineno #=> 0
f.readline #=> "This is line one\n"
f.rewind.readline #=> "This is line one\n"
Proxy for Dir#rewind
ario.rewind => ario
Repositions ario to the first entry.
d = rio("testdir")
d.read #=> rio(".")
d.rewind.read #=> rio(".")
167 |
# File 'lib/rio/if/fileordir.rb', line 167 def rewind(&block) target.rewind(&block); self end |
#seek(*args) ⇒ Object
For directories calls Dir#seek, otherwise calls IO#seek
For streams calls IO#seek
ario.seek(amount, whence=SEEK_SET) -> ario
Seeks to a given offset amount in the stream according to the value of whence:
IO::SEEK_CUR | Seeks to 'amount' plus current position
--------------+----------------------------------------------------
IO::SEEK_END | Seeks to 'amount' plus end of stream (you probably
| want a negative value for 'amount')
--------------+----------------------------------------------------
IO::SEEK_SET | Seeks to the absolute location given by 'amount'
Example:
f = rio("testfile")
f.seek(-28, IO::SEEK_END).readline #=> "happily ever after. The End\n"
For directories calls Dir#seek
ario.seek( integer ) => ario
Seeks to a particular location in ario. integer must be a value returned by IF::FileOrDir#tell.
d = rio("testdir") #=> #<RIO::Rio:0x401b3c40>
d.read #=> rio(".")
i = d.tell #=> 12
d.read #=> rio("..")
d.seek(i) #=> #<RIO::Rio:0x401b3c40>
d.read #=> rio("..")
199 |
# File 'lib/rio/if/fileordir.rb', line 199 def seek(*args) target.seek(*args); self end |
#symlink(dest) ⇒ Object
Creates a symbolic link dest which points to the Rio’s IF::Path#fspath.
Raises a NotImplementedError exception on platforms that do not support symbolic links. dest may be a Rio, a String, or anything that will create an appropriate Rio when passed to Rio#new . If dest already exists and is a directory, creates a symbolic link in the dest directory, named with the name returned by IF::Path#filename. If dest already exists and it is not a directory, raises Errno::EEXIST.
Returns the Rio (not the symlink).
IF::FileOrDir#symlink differs from File#symlink when the Rio or the dest path has directory information. In this case IF::FileOrDir#symlink creates a symlink that actually refers to the Rio’s location from the perspective of the link’s location.
For example: Given an existing file ‘adir/afile’ and a dest of ‘adir/alink’
::File.symlink('adir/afile','adir/alink1') # creates 'adir/alink1 -> adir/afile'
::File.exist?('adir/alink1') # false
rio('adir/afile').symlink('adir/alink2') # creates 'adir/alink2 -> afile'
::File.exist?('adir/alink2') # true
To replace an existing symlink use the following Rio idiom
rio('afile').symlink( rio('link_name').delete ) # delete 'link_name' and recreate linked to 'afile'
Examples
rio('afile').symlink('alink') # create the symbolic link 'alink' which references 'afile'
rio('afile').symlink('adir/alink') # create a symlink 'adir/alink' -> '../afile'
rio('adir/afile').symlink('alink') # create a symlink 'alink' -> 'adir/afile'
rio('adir/afile').symlink('adir/alink') # create a symlink 'adir/alink' -> 'afile'
rio('adir/afile').symlink('adir/alink') # create a symlink 'adir/alink' -> 'afile'
rio('adir1/afile').symlink('adir2/alink') # create a symlink 'adir2/alink' -> '../adir1/afile'
rio('/tmp/afile').symlink('alink') # create a symlink 'adir/alink' -> '/tmp/afile'
75 |
# File 'lib/rio/if/fileordir.rb', line 75 def symlink(dest) target.symlink(dest); self end |
#tell ⇒ Object
See IF::FileOrDir#pos
230 |
# File 'lib/rio/if/fileordir.rb', line 230 def tell() target.tell end |