Module | RIO::Doc::HOWTO | lib/rio/doc/HOWTO.rb |
Rio is a convenience class wrapping much of the functionality of IO, File, Dir, Pathname, FileUtils, Tempfile, StringIO, OpenURI, Zlib, and CSV.
ario = rio('afile') string = "" array = []
# method 1 string = ario.slurp # method 2 ario > string
# method 1 ario >> string # method 2 string += ario.slurp
# method 1 array = ario[] # method 2 ario > array # method 3 array = ario.to_a # method 4 array = ario.readlines
# method 1 ario >> array # method 2 array += ario.lines[]
# method 1 array = ario[0...10] # method 2 array = ario.lines[0...10] # method 3 ario.lines(0...10) > array
# method 1 array = ario.chomp[] # method 2 array = ario.chomp.lines[] # method 3 ario.chomp > array
# method 1 array += ario.chomp.lines[0...10] # method 2 ario.chomp.lines(0...10) >> array
# method 1 array = ario.chomp.lines(/^\s*require/) # method 2 ario.chomp.lines(/^\s*require/) > array
# method 1 rio('afile.gz').gzip > string # method 2 string = rio('afile.gz').gzip.slurp
# method 1 rio('afile.gz').gzip >> string # method 2 string += rio('afile.gz').gzip.slurp
# method 1 rio('afile').lines { |line| ... } # method 2 rio('afile').each { |line| ... } # method 3 rio('afile').each_line { |line| ... }
rio('afile.gz').gzip { |line| ... }
rio('afile.gz').gzip.chomp.nolines(:empty?) { |line| ... }
# method 1 rio('afile').lines(0...100) { |line| ... }
rio('afile.rb.gz').gzip.lines(0,/^\s*#/) { |line| ... }
rio('afile.rb.gz').chomp.nolines(/^\s*#/,:empty?) { |line| ... }
# method 1 array = rio('afile.rb').chomp.lines[/^\s*#/] # method 2 rio('afile.rb').chomp.lines(/^\s*#/) > array
# method 1 ario.chomp.lines(proc{ |line| line.length <= 1024}) > array # method 2 array = ario.chomp.nolines[proc{ |line| line.length > 1024}] # method 3 array = ario.chomp.lines(proc{ |line| line.length <= 1024}).to_a
ario = rio('afile') string = "A String\n" array = ["Line 0\n","Line 1\n"]
# method 1 ario.puts(string) # method 2 ario.print(string) # method 3 ario.noclose < string
# method 1 rio('afile') < string # method 2 ario.print!(string) # method 3 ario.print(string).close
# method 1 ario.a.puts(string) # method 2 ario.a.print(string) # method 3 ario.noclose << string
# method 1 rio('afile') << string # method 2 rio('afile').a.print!(string) # method 3 rio('afile').a.print(string).close
# method 1 ario = rio('afile').nocloseoncopy ario << array # method 2 ario.noclose < array
# method 1 rio('afile') < array
ario = rio('afile') string = "" array = []
# method 1 array = ario[0..9,99] # method 2 array = ario[0...10,99..99] # method 3 ario(0..9,99) > array
# method 1 array = ario[0..9,99,/^zippy/] # method 2 array = ario[0...10,99..99,/^zippy/] # method 3 ario(0..9,99,/^zippy/) > array
# method 1 array = ario[proc{ |l| l.length > 128}] # method 2 array = ario.lines[proc{ |l| l.length > 128}] # method 3 array = ario.nolines[proc{ |l| l.length <= 128}]
# method 1 ario.nolines(/^zippy/) > rio('another_file') # method 2 ario.lines.nolines(/^zippy/) > rio('another_file') # method 3 rio('another_file') < ario.nolines(/^zippy/)
# method 1 ario.lines(0...10,/^zippy/).nolines(proc{ |l| l.length > 128}] > rio('another_file') # method 2 rio('another_file') < ario.lines(0...10,/^zippy/).nolines(proc{ |l| l.length > 128})
ario = rio('adir') string = "" array = []
# method 1 array = ario['*.txt'] # method 2 array = ario[/\.txt/] # method 3 array = ario.entries['*.txt']
# method 1 array = ario.files['*.txt'] # method 2 array = ario.files[/\.txt/] # method 3 array = ario.files['*.txt']
# method 1 array = ario.all['*.txt'] # method 2 array = ario.all[/\.txt/] # method 3 array = ario.all.entries['*.txt']
# method 1 array = ario.norecurse('.svn').all['*.txt'] # method 2 array = ario.norecurse(/^\.svn$/).all[/\.txt/] # method 3 array = ario.norecurse('.svn').entries['*.txt'] # method 4 array = ario.entries('*.txt').norecurse('.svn').to_a # method 5 array = ario.norecurse('.svn')['*.txt']
# method 1 ario.norecurse('.svn','pkg').files('*.rb',proc{ |f| f.executable? and f[0] =~ /^#!.+ruby/ }) { |f| ... }
# method 1 array = ario.nofiles[:symlink?] # method 2 array = ario.nofiles(:symlink?).files[] # method 3 array = ario.nofiles(:symlink?).to_a # method 4 array = ario.files.nofiles[:symlink?]
# method 1 array = ario.nofiles[] # method 2 array = ario.nofiles.to_a
# method 1 array = ario.files[proc{|f| f.file? and f.symlink?}] # method 2 array = ario.files(proc{|f| f.file? and f.symlink?}).to_a
# method 1 array = ario.nodirs['.svn'] # method 2 array = ario.nodirs[/^\.svn/] # method 3 array = ario.nodirs('.svn').to_a # method 4 array = ario.nodirs('.svn').dirs[] # method 5 array = ario.nodirs('.svn')[]
ario = rio('afile') string = "" array = []
# method 1 rio('srcfile') > rio('dstfile') # method 2 rio('dstfile') < rio('srcfile') # method 3 rip('dstfile').print!(rio('srcfile').slurp)
# method 1 rio('srcfile') >> rio('dstfile') # method 2 rio('dstfile') << rio('srcfile') # method 3 rip('dstfile').a.print!(rio('srcfile').slurp)
# method 1 rio('srcfile').lines(0...10) > rio('dstfile') # method 2 rio('dstfile') < rio('srcfile').lines(0...10) # method 3 rio('dstfile') < rio('srcfile').lines[0...10]
# method 1 rio('dstfile') < [ rio('src1'), rio('src2'), rio('src3') ] # method 2 rio('dstfile') < rio('src1') << rio('src2') << rio('src3')
# method 1 rio('http://ruby-doc.org/') > rio('afile') # method 2 rio('afile') < rio('http://ruby-doc.org/') # method 3 rio('afile').print!(rio('http://ruby-doc.org/').slurp)
# method 1 rio("tcp://localhost:daytime") >> rio('afile') # method 2 rio("tcp:",'localhost','daytime') >> rio('afile') # method 3 rio('afile') << rio("tcp://:daytime") # method 4 rio('afile') << rio("tcp://:13")
# method 1 rio('srcfile').lines(0,/http:/) > rio('dstfile') # method 2 rio('dstfile') < rio('srcfile').lines(0,/http:/) # method 3 rio('dstfile') < rio('srcfile').lines[0,/http:/] # method 4
# method 1 rio('afile') > rio('afile.gz').gzip # method 2 rio('afile.gz').gzip < rio('afile') # method 3 rio('afile.gz').gzip.print!( rio('afile').slurp )
# method 1 rio('afile') < rio('afile.gz').gzip # method 2 rio('afile.gz').gzip > rio('afile') # method 3 rio('afile').print!( rio('afile.gz').gzip.slurp )
# method 1 rio('http://aserver/afile.gz').gzip.lines(0...100) > rio('afile')
# method 1 rio('out') < [ rio('header'), rio(?-,'ps'), "Created on ", rio('tcp://:daytime') ] # method 2 rio('out') < rio('header') << rio(?-,'ps') << "Created on: " << rio("tcp://:daytime")
ario = rio('adir') string = "" array = []
# method 1 cnt = ario.all.files('*.rb').chomp.nolines(/^\s*#/,/^\s*$/).inject(0) { |sum,l| sum += 1 } # method 2 cnt = ario.all.files('*.rb').chomp.nolines[/^\s*#/,/^\s*$/].size
# method 1 array = ario.lines.files['*.txt'] # method 2 array = ario.files('*.txt').lines[] # method 3 ario.files('*.txt').lines > array
# method 1 array = ario.lines(0).files['*.txt'] # method 2 array = ario.files('*.txt').lines[0] # method 3 ario.files('*.txt').lines(0) > array
# method 1 ario.files('*.txt').lines(0...10) > rio('another_dir')
string = "" array = []
# method 1 ans = rio(?-).chomp.print("Type Something: ").gets # method 2 stdio = rio(?-).chomp ans = stdio.print("Type Something: ").gets
stdio = rio(?-)
stderr = rio(?=)
# method 1 rio(?-).puts("Hello World") # method 2 rio(?-) << "Hello World\n" # method 3 rio(?-) < "Hello World\n"
# method 1 ans = rio(?-).chomp.gets # method 2 stdio = rio(?-).chomp ans = stdio.gets
# method 1 rio(?-) >> string # method 2 rio(?-) > string
# method 1 rio(?-).chomp >> array # method 2 rio(?-).chomp > array
# method 1 rio(?-) > rio('afile') # method 2 rio('afile') < rio(?-)
# method 1 rio(?-) >> rio('afile') # method 2 rio('afile') << rio(?-)
# method 1 rio(?=).puts("Hello Error") # method 2 rio(?=) << "Hello Error\n" # method 3 rio(?=) < "Hello Error\n"
# method 1 rio(?-) << rio('afile') # method 2 rio('afile') >> rio(?-) # method 3 rio('afile') > rio(?-) # method 4 rio(?-) < rio('afile') # method 5 rio(?-).print(rio('afile').slurp)
# method 1 rio(?-).lines(0..9) > rio(?-)
ps = rio(?-,'ps -a').nolines[0,/ps$/]
# method 1 ans = "" cat = rio(?-,'cat').w! cat <"Hello Kitty\n" >ans
string = "" array = []
# method 1 rio('a').rename('b') # method 2 rio('a').rename.filename = 'b'
ario = rio('a') # method 1 ario.rename('b')
ario = rio('a') # method 1 ario.rename!('b')
# method 1 rio('index.htm').rename('index.html') # method 2 rio('index.htm').rename.extname = '.html'
# method 1 rio('index.html').rename('welecome.html') # method 2 rio('index.htm').rename.basename = 'welcome'
# method 1 rio('src/afile').rename('dst/afile') # method 2 rio('src/afile').rename.dirname = 'dst'
# method 1 ario.rename.extname = '.html'
# method 1 ario.rename.basename = 'zippy'
# method 1 ario.rename.ext('.tar.gz').extname = '.tgz'
# method 1 rio('adir').rename.files('*.htm') do |htmfile| htmlfile.extname = '.html' end # method 2 rio('adir').files('*.htm') do |htmfile| htmlfile.rename.extname = '.html' end
# method 1 rio('adir').rename.all.files('*.htm') do |htmfile| htmfile.extname = '.html' end # method 2 rio('adir').all.files('*.htm') do |htmfile| htmfile.rename.extname = '.html' end
string = "" array = []
ap = rio('adir') # method 1 ap /= 'subdirectory' # method 2 ap = ap.join('subdirectory') # method 3 ap = rio(ap,'subdirectory') # method 4
dirs = ['adir','subdir1','subdir2'] # method 1 ario = rio(dirs)
# method 1 anarray = rio('adir/subdir1/subdir2').split
# method 1 ario = rio('apath') + astring # method 2 ario = rio('apath') ario += astring
lib = rio('lib') links = rio('links').delete!.mkdir lib.all.files("*.rb") do |f| f.symlink( f.dirname.sub(/^#{lib}/,links).mkdir ) end
Suggested Reading
Copyright © 2005 Christopher Kleckner. All rights reserved.