Module: RIO

Includes:
Ops::Construct
Defined in:
lib/rio.rb,
lib/rio.rb,
lib/rio/cp.rb,
lib/rio/cp.rb,
lib/rio/cp.rb,
lib/rio/cp.rb,
lib/rio/cp.rb,
lib/rio/cp.rb,
lib/rio/if.rb,
lib/rio/if.rb,
lib/rio/def.rb,
lib/rio/dir.rb,
lib/rio/doc.rb,
lib/rio/ext.rb,
lib/rio/ext.rb,
lib/rio/ext.rb,
lib/rio/ext.rb,
lib/rio/ioh.rb,
lib/rio/argv.rb,
lib/rio/base.rb,
lib/rio/file.rb,
lib/rio/path.rb,
lib/rio/util.rb,
lib/rio/const.rb,
lib/rio/local.rb,
lib/rio/match.rb,
lib/rio/open3.rb,
lib/rio/piper.rb,
lib/rio/state.rb,
lib/rio/assert.rb,
lib/rio/ext/if.rb,
lib/rio/filter.rb,
lib/rio/filter.rb,
lib/rio/filter.rb,
lib/rio/fs/url.rb,
lib/rio/ftp/fs.rb,
lib/rio/grande.rb,
lib/rio/handle.rb,
lib/rio/if/csv.rb,
lib/rio/iomode.rb,
lib/rio/iowrap.rb,
lib/rio/kernel.rb,
lib/rio/nullio.rb,
lib/rio/prompt.rb,
lib/rio/rl/ioi.rb,
lib/rio/rl/uri.rb,
lib/rio/stream.rb,
lib/rio/to_rio.rb,
lib/rio/arycopy.rb,
lib/rio/callstr.rb,
lib/rio/context.rb,
lib/rio/ext/csv.rb,
lib/rio/ext/csv.rb,
lib/rio/ext/csv.rb,
lib/rio/factory.rb,
lib/rio/fs/base.rb,
lib/rio/fs/impl.rb,
lib/rio/ftp/dir.rb,
lib/rio/if/file.rb,
lib/rio/if/file.rb,
lib/rio/if/temp.rb,
lib/rio/if/yaml.rb,
lib/rio/no_warn.rb,
lib/rio/ops/dir.rb,
lib/rio/ops/dir.rb,
lib/rio/ops/dir.rb,
lib/rio/ops/dir.rb,
lib/rio/ops/dir.rb,
lib/rio/rectype.rb,
lib/rio/rl/base.rb,
lib/rio/rl/path.rb,
lib/rio/version.rb,
lib/rio/entrysel.rb,
lib/rio/entrysel.rb,
lib/rio/ext/yaml.rb,
lib/rio/if/basic.rb,
lib/rio/ios/fail.rb,
lib/rio/ios/fail.rb,
lib/rio/ios/mode.rb,
lib/rio/ios/null.rb,
lib/rio/ios/null.rb,
lib/rio/ios/null.rb,
lib/rio/ops/file.rb,
lib/rio/ops/path.rb,
lib/rio/ops/path.rb,
lib/rio/piper/cp.rb,
lib/rio/rl/chmap.rb,
lib/rio/construct.rb,
lib/rio/construct.rb,
lib/rio/doc/HOWTO.rb,
lib/rio/doc/INTRO.rb,
lib/rio/exception.rb,
lib/rio/fs/native.rb,
lib/rio/fs/stream.rb,
lib/rio/if/grande.rb,
lib/rio/if/grande.rb,
lib/rio/if/string.rb,
lib/rio/if/string.rb,
lib/rio/if/string.rb,
lib/rio/rl/fs2url.rb,
lib/rio/scheme/fd.rb,
lib/rio/symantics.rb,
lib/rio/ops/create.rb,
lib/rio/ops/either.rb,
lib/rio/ops/stream.rb,
lib/rio/path/reset.rb,
lib/rio/rl/builder.rb,
lib/rio/scheme/ftp.rb,
lib/rio/scheme/tcp.rb,
lib/rio/constructor.rb,
lib/rio/context/cxx.rb,
lib/rio/context/dir.rb,
lib/rio/ext/mp3info.rb,
lib/rio/ext/mp3info.rb,
lib/rio/ext/zipfile.rb,
lib/rio/ext/zipfile.rb,
lib/rio/filter/gzip.rb,
lib/rio/ftp/ftpfile.rb,
lib/rio/if/internal.rb,
lib/rio/ios/generic.rb,
lib/rio/ios/generic.rb,
lib/rio/matchrecord.rb,
lib/rio/ops/symlink.rb,
lib/rio/rl/withpath.rb,
lib/rio/rl/withpath.rb,
lib/rio/rl/withpath.rb,
lib/rio/scheme/http.rb,
lib/rio/scheme/null.rb,
lib/rio/scheme/path.rb,
lib/rio/scheme/temp.rb,
lib/rio/state/error.rb,
lib/rio/stream/base.rb,
lib/rio/stream/open.rb,
lib/rio/stream/open.rb,
lib/rio/context/gzip.rb,
lib/rio/context/skip.rb,
lib/rio/doc/EXAMPLES.rb,
lib/rio/doc/OPTIONAL.rb,
lib/rio/doc/SYNOPSIS.rb,
lib/rio/ext/yaml/doc.rb,
lib/rio/ext/yaml/tie.rb,
lib/rio/ext/yaml/tie.rb,
lib/rio/ext/yaml/tie.rb,
lib/rio/if/fileordir.rb,
lib/rio/if/fileordir.rb,
lib/rio/scheme/cmdio.rb,
lib/rio/scheme/stdio.rb,
lib/rio/scheme/strio.rb,
lib/rio/scheme/sysio.rb,
lib/rio/ftp/conncache.rb,
lib/rio/ops/construct.rb,
lib/rio/ops/construct.rb,
lib/rio/scheme/stderr.rb,
lib/rio/stream/duplex.rb,
lib/rio/context/stream.rb,
lib/rio/exception/copy.rb,
lib/rio/exception/open.rb,
lib/rio/ext/splitlines.rb,
lib/rio/ext/splitlines.rb,
lib/rio/ext/zipfile/fs.rb,
lib/rio/ext/zipfile/rl.rb,
lib/rio/rl/pathmethods.rb,
lib/rio/scheme/cmdpipe.rb,
lib/rio/context/copying.rb,
lib/rio/context/methods.rb,
lib/rio/context/methods.rb,
lib/rio/context/methods.rb,
lib/rio/context/methods.rb,
lib/rio/context/methods.rb,
lib/rio/exception/state.rb,
lib/rio/if/grande_entry.rb,
lib/rio/if/grande_entry.rb,
lib/rio/ops/stream/read.rb,
lib/rio/ext/zipfile/wrap.rb,
lib/rio/if/grande_stream.rb,
lib/rio/if/grande_stream.rb,
lib/rio/ops/stream/input.rb,
lib/rio/ops/stream/input.rb,
lib/rio/ops/stream/write.rb,
lib/rio/context/autoclose.rb,
lib/rio/ext/zipfile/state.rb,
lib/rio/filter/closeoneof.rb,
lib/rio/ops/stream/output.rb,
lib/rio/ops/stream/output.rb,
lib/rio/ext/zipfile/rootdir.rb,
lib/rio/exception/notsupported.rb,
lib/rio/exception/notimplemented.rb,
lib/rio/exception/notimplemented.rb

Overview

def self.generate_line( array, field_sep=nil, row_sep=nil )

    FasterCSV.generate_line( array, :col_sep => field_sep || ",",
                                    :row_sep => row_sep   || "" )
  end
end

rescue LoadError

require 'csv'         # second choice--slower but standard

end

Defined Under Namespace

Modules: AryCopy, Assert, CallStr, CmdIO, CmdPipe, Cp, Cx, Dir, Doc, Error, Exception, Ext, FD, FS, FTP, File, Filter, Grande, HTTP, IF, IOH, IOS, IOWrap, Impl, Local, Match, Mode, Null, Ops, Path, Piper, RL, RecType, State, StdErr, StdIO, StrIO, Stream, Symantics, SysIO, TCP, Temp, ToRio, Util, Version, ZipFile Classes: Base, Factory, GenericIOMode, Handle, NotSupportedException, NullIO, NullIOMode, Rio

Constant Summary collapse

TITLE =
"Rio"
SUMMARY =
"Rio - Ruby I/O Comfort Class"
DESCRIPTION =
"Rio is a Ruby class wrapping much of the functionality of " +
                "IO, File, Dir, Pathname, FileUtils, Tempfile, StringIO, OpenURI, Zlib, and CSV.
"
SEEK_SET =
IO::SEEK_SET
SEEK_END =
IO::SEEK_END
SEEK_CUR =
IO::SEEK_CUR
FILTER_SYMS =
[:chomp, :strip, :lstrip, :rstrip]
VERSION =
Version::VERSION
CONSTRUCTOR_SYMS =
[
  :strio,
  :stdio,
  :stderr,
  :temp,
  :tempfile,
  :tempdir,
  :tcp,
  :cmdio,
  :cmdpipe,
  :sysio,
  :fd,
]

Class Method Summary collapse

Class Method Details

.ARGVObject



41
42
43
# File 'lib/rio/argv.rb', line 41

def ARGV()
  ::ARGV.map { |path| rio(path) }
end

.cwd(*args, &block) ⇒ Object

Create a Rio as with RIO#rio which refers to the current working directory

wd = RIO.cwd

If passed arguments they are treated as if

rio(RIO.cwd,*args)

had been called



235
236
237
# File 'lib/rio/constructor.rb', line 235

def cwd(*args,&block)  # :yields: self
  Rio.new.getwd(*args,&block) 
end

.no_warnObject

Based on a suggestion posted by Andrew L. Johnson to comp.lang.ruby



39
40
41
42
43
44
45
46
47
# File 'lib/rio/no_warn.rb', line 39

def no_warn
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    yield
  ensure
    $VERBOSE = verbose
  end
end

.popen3(*args, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rio/open3.rb', line 51

def popen3(*args,&block)
  if block_given?
    i,o,e = nil,nil,nil
    begin
      Open3.popen3(*args) do |si,so,se|
        yield(i=Rio.new(si),o=Rio.new(so),e=Rio.new(se))
      end
    ensure
      [i,o,e].each { |el| el.reset if el }
    end
  else
    si,so,se = Open3.popen3(*args)
    [Rio.new(si),Rio.new(so),Rio.new(se)]
  end
end

.prompt(str = "") ⇒ Object



41
42
43
# File 'lib/rio/prompt.rb', line 41

def prompt(str="")
  rio(?-).strip.print(str).gets
end

.promptd(str = "", default = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/rio/prompt.rb', line 44

def promptd(str="",default=nil)
  dstr = default ? "[#{default}]: " : ": "
  pstr = str + dstr
  ans = prompt(pstr)
  ans.empty? ? default : ans
end

.rio(*args, &block) ⇒ Object

Rio Constructor

For purposes of discussion, we divide Rios into two catagories, those that have a path and those that don’t.

Creating a Rio that has a path

To create a Rio that has a path the arguments to rio may be:

  • a string representing the entire path. The separator used for Rios is as specified in RFC1738 (‘/’).

    rio('adir/afile')
    
  • a string representing a fully qualified file URL as per RFC1738

    rio('file:///atopleveldir/adir/afile')
    
  • a URI object representing a file or generic URL

    rio(URI('adir/afile'))
    
  • the components of a path as separate arguments

    rio('adir','afile')
    
  • the components of a path as an array of separate arguments

    rio(%w/adir afile/)
    
  • another Rio

    another_rio = rio('adir/afile')
    rio(another_rio)
    
  • any object whose to_s method returns one of the above

    rio(Pathname.new('apath'))
    
  • any combination of the above either as separate arguments or as elements of an array,

    another_rio = rio('dir1/dir2')
    auri = URI('dir4/dir5)
    rio(another_rio,'dir3',auri,'dir6/dir7')
    
Creating a Rio that refers to a web page

To create a Rio that refers to a web page the arguments to rio may be:

  • a string representing a fully qualified http URL

    rio('http://ruby-doc.org/index.html')
    
  • a URI object representing a http URL

    rio(URI('http://ruby-doc.org/index.html'))
    
  • either of the above with additional path elements

    rio('http://www.ruby-doc.org/','core','classes/Object.html')
    
Creating a Rio that refers to a file or directory on a FTP server

To create a Rio that refers to a file on a FTP server the arguments to rio may be:

  • a string representing a fully qualified ftp URL

    rio('ftp://user:[email protected]/afile.tar.gz')
    
  • a URI object representing a ftp URL

    rio(URI('ftp://ftp.example.com/afile.tar.gz'))
    
  • either of the above with additional path elements

    rio('ftp://ftp.gnu.org/pub/gnu','emacs','windows','README')
    

Creating Rios that do not have a path

To create a Rio without a path, the first argument to rio is usually either a single character or a symbol.

Creating a Rio that refers to a clone of your programs stdin or stdout.

rio(?-) (mnemonic: ‘-’ is used by some Unix programs to specify stdin or stdout in place of a file)

rio(:stdio)

Just as a Rio that refers to a file, does not know whether that file will be opened for reading or writing until an I/O operation is specified, a stdio: Rio does not know whether it will connect to stdin or stdout until an I/O operation is specified.

Currently :stdin and :stdout are allowed as synonyms for :stdio. This allows one to write

rio(:stdout).puts("Hello :stdout")

which is reasonable. It also allows one to write

rio(:stdin).puts("Hello :stdin")

which is not reasonable and will be disallowed in future releases.

Creating a Rio that refers to a clone of your programs stderr.

rio(?=) (mnemonic: ‘-’ refers to fileno 1, so ‘=’ refers to fileno 2)

rio(:stderr)

Creating a Rio that refers to an arbitrary IO object.
an_io = ::File.new('afile')
rio(an_io)
Creating a Rio that refers to a file descriptor

rio(?#,file_descriptor) (mnemonic: a file descriptor is a number ‘#’)

rio(:fd,file_descriptor)

an_io = ::File.new('afile')
fnum = an_io.fileno
rio(?#,fnum)
Creating a Rio that refers to a StringIO object

rio(?") (mnemonic: ‘“’ surrounds strings)

rio(:string)

rio(:strio)

rio(:stringio)

  • create a Rio that refers to a string that it creates

    rio(?")
    
  • create a Rio that refers to a string of your choosing

    astring = ""
    rio(?",astring)
    
Creating a Rio that refers to a temporary object

To create a temporary object that will become a file (Tempfile) or a temporary directory, depending on how it is used.

rio(??) (mnemonic: ‘?’ you don’t know its name)

rio(:temp)

The following are also supported, to specify file or directory

rio(:tempfile)

rio(:tempdir)

rio(??)
rio(??,basename='rio',tmpdir=Dir::tmpdir)

To create a temporary object that will become a file or a directory, depending on how you use it:

rio(??)
rio(??,basename='rio',tmpdir=Dir::tmpdir)

To force it to become a file

rio(??).file

or just write to it.

To force it to become a directory:

rio(??).mkdir

or

rio(??).chdir
Creating a Rio that refers to an arbitrary TCPSocket

rio('tcp:',hostname,port)

rio('tcp://hostname:port')

rio(:tcp,hostname,port)

Creating a Rio that runs an external program and connects to its stdin and stdout

rio(?-,cmd) (mnemonic: ‘-’ is used by some Unix programs to specify stdin or stdout in place of a file)

rio(?`,cmd) (mnemonic: ‘`’ (backtick) runs an external program in ruby)

rio(:cmdio,cmd)

This is Rio’s interface to IO#popen

Creating a Rio that acts like /dev/null

rio(nil)

rio(:null)

This rio behaves like the Unix file /dev/null, but does depend on it - and thus will work on non-Unix systems. Reading behaves as if reading from an empty file, and writing to it discards anything written.

Creating a Rio Pipe

A Rio Pipe is a sequence of Rios that are run with the output of each being copied to the input of the next.

rio(?|, ario, ...) (mnemonic: ‘|’ is the Unix pipe operator)

rio(:cmdpipe, ario, ...)

See also IF::Grande#|



225
226
227
# File 'lib/rio/constructor.rb', line 225

def rio(*args,&block)  # :yields: self
  Rio.rio(*args,&block) 
end

.root(*args, &block) ⇒ Object

Create a Rio as with RIO#rio which refers to a directory at the root of the file system

tmpdir = RIO.root('tmp') #=> rio('/tmp')


242
243
244
# File 'lib/rio/constructor.rb', line 242

def root(*args,&block) # :yields: self
  Rio.new.rootpath(*args,&block) 
end