| Module | RIO::Doc::HOWTO | lib/rio/doc/HOWTO.rb |
Rio is a facade for most of the standard ruby classes that deal with I/O; providing a simple, intuitive, succinct interface to the functionality provided by IO, File, Dir, Pathname, FileUtils, Tempfile, StringIO, OpenURI and others. Rio also provides an application level interface which allows many common I/O idioms to be expressed succinctly.
ario = rio('afile')
string = ""
array = []
# method 1 string = ario.contents # method 2 ario > string
# method 1 ario >> string # method 2 string += ario.contents
# 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[0...10] # method 2 array += ario.chomp.lines[0...10] # method 3 ario.chomp.lines(0...10) >> array
# method 1 array = ario.chomp[/^\s*require/] # method 2 array = ario.chomp.lines[/^\s*require/] # method 3 ario.chomp.lines(/^\s*require/) > array
# method 1
rio('afile.gz').gzip > string
# method 2
string = rio('afile.gz').gzip.contents
# method 1
rio('afile.gz').gzip >> string
# method 2
string += rio('afile.gz').gzip.contents
# 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.skiplines(:empty?) { |line| ... }
# method 1
rio('afile').lines(0...100) { |line| ... }
rio('afile.rb.gz').gzip.lines(0,/^\s*#/) { |line| ... }
rio('afile.rb').chomp.skiplines(/^\s*#/,:empty?) { |line| ... }
# method 1
array = rio('afile.rb').chomp[/^\s*#/]
# method 2
array = rio('afile.rb').chomp.lines[/^\s*#/]
# method 3
rio('afile.rb').chomp.lines(/^\s*#/) > array
# method 1
array = ario.chomp[proc{ |line| line.length <= 1024}]
# method 2
ario.chomp.lines(proc{ |line| line.length <= 1024}) > array
# method 3
array = ario.chomp.skiplines[proc{ |line| line.length > 1024}]
# method 4
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.noautoclose < 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.noautoclose << 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.noautoclose < array
# method 1
rio('afile') < array
ario = rio('afile')
string = ""
array = []
# method 1 array = ario[0..9,99] # method 2 array = ario.lines[0..9,99] # method 3 ario.lines(0..9,99) > array
# method 1 array = ario[0..9,99,/^rio4ruby/] # method 2 array = ario.lines[0..9,99,/^rio4ruby/] # method 3 ario.lines(0..9,99,/^rio4ruby/) > array
# method 1
array = ario[proc{ |l| l.length > 128}]
# method 2
array = ario.lines[proc{ |l| l.length > 128}]
# method 3
array = ario.skiplines[proc{ |l| l.length <= 128}]
# method 4
array = ario.skip.lines[proc{ |l| l.length <= 128}]
# method 1
ario.skiplines(/^rio4ruby/) > rio('another_file')
# method 2
ario.lines.skiplines(/^rio4ruby/) > rio('another_file')
# method 3
rio('another_file') < ario.skiplines(/^rio4ruby/)
# method 1
ario.lines(0...10,/^rio4ruby/).skiplines(proc{ |l| l.length > 128}] > rio('another_file')
# method 2
rio('another_file') < ario.lines(0...10,/^rio4ruby/).skiplines(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 array = ario.dirs[] # method 2 array = ario.dirs.to_a
# method 1 array = ario.all.dirs[] # method 2 array = ario.all.dirs.to_a
# method 1 array = ario.norecurse(3).to_a
# method 1
is_ruby_exe = proc{ |f| f.executable? and f[0][0] =~ /^#!.+ruby/ }
ario.norecurse('.svn','pkg').files('*.rb',is_ruby_exe) { |f| ... }
# method 2
is_ruby_exe = proc{ |f| f.executable? and f.gets =~ /^#!.+ruby/ }
ario.norecurse('.svn','pkg').files('*.rb',is_ruby_exe) { |f| ... }
# method 1 array = ario.skipfiles[:symlink?] # method 2 array = ario.skipfiles(:symlink?).files[] # method 3 array = ario.skipfiles(:symlink?).to_a # method 4 array = ario.files.skipfiles[:symlink?]
# method 1 array = ario.skipfiles[] # method 2 array = ario.skipfiles.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.skipdirs['.svn']
# method 2
array = ario.skipdirs[/^\.svn$/]
# method 3
array = ario.skipdirs('.svn').to_a
# method 4
array = ario.skipdirs('.svn').dirs[]
# method 5
array = ario.skipdirs('.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').contents)
# method 1
rio('srcfile') >> rio('dstfile')
# method 2
rio('dstfile') << rio('srcfile')
# method 3
rip('dstfile').a.print!(rio('srcfile').contents)
# 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/').contents)
# 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').contents )
# 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.contents )
# 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').skiplines[/^\s*#/,/^\s*$/].size
# method 2
cnt = ario.all.files('*.rb').skiplines(/^\s*#/,/^\s*$/).inject(0) { |sum,l| sum += 1 }
# 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('afile') > ?-
# method 2
rio('afile') > rio(?-)
# method 3
rio(?-) << rio('afile')
# method 4
rio('afile') >> ?-
# method 5
rio(?-) < rio('afile')
# method 6
rio(?-).print(rio('afile').contents)
# method 1 rio(?-).lines(0..9) > ?-
ps = rio(?-,'ps -a').skiplines[0,/ps$/]
infile = rio(?","Hello Kitty\n")
outfile = rio('out.txt')
# method 1
cat = rio(?-,'cat').w!
cat <infile >outfile
# method 2
infile | 'cat' | outfile
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 = 'rio4ruby'
# 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
# method 1
rio('arb/i/trary/di/rec/tory/afile').rename.dirname = '.'
string = "" array = []
ap = rio('adir')
# method 1
ap /= 'subdirectory'
# method 2
ap = ap.join('subdirectory')
# method 3
ap = rio(ap,'subdirectory')
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,2006,2007 Christopher Kleckner. All rights reserved.