Module: RIO::IF::FileOrDir

Included in:
Rio
Defined in:
lib/rio/if/fileordir.rb

Instance Method Summary collapse

Instance Method Details

#open(m, *args, &block) ⇒ Object

undocumented



29
# File 'lib/rio/if/fileordir.rb', line 29

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 #seek.

d = rio("testdir")
d.pos   #=> 0
d.read  #=> rio(".")
d.pos   #=> 12


214
# File 'lib/rio/if/fileordir.rb', line 214

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 #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("..")


243
# File 'lib/rio/if/fileordir.rb', line 243

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")



128
# File 'lib/rio/if/fileordir.rb', line 128

def read(*args) target.read(*args)end

Calls File#readlink

Returns a Rio referencing the file referenced by the given link. Not available on all platforms.



69
# File 'lib/rio/if/fileordir.rb', line 69

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 #dirname=, #filename=, #basename=, and #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 #dirname=, #filename=, #basename=, and #extname=



96
# File 'lib/rio/if/fileordir.rb', line 96

def rename(*args,&block) target.rename(*args,&block); self end

#rename!(*args, &block) ⇒ Object

Behaves like #rename, but also changes the calling Rio to refer to the renamed path



101
# File 'lib/rio/if/fileordir.rb', line 101

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.



248
# File 'lib/rio/if/fileordir.rb', line 248

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(".")



154
# File 'lib/rio/if/fileordir.rb', line 154

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 #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("..")



186
# File 'lib/rio/if/fileordir.rb', line 186

def seek(*args) target.seek(*args); self end

Creates a symbolic link dest which points to the Rio's #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 #filename. If dest already exists and it is not a directory, raises Errno::EEXIST.

Returns the Rio (not the symlink).

#symlink differs from File#symlink when the Rio or the dest path has directory information. In this case #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'



62
# File 'lib/rio/if/fileordir.rb', line 62

def symlink(dest) target.symlink(dest); self end

#tell ⇒ Object

See #pos



217
# File 'lib/rio/if/fileordir.rb', line 217

def tell() target.tell end