Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
{ANN] Rio 0.4.0
rio4ruby
4/3/2007 3:08:00 AM
Rio 0.4.0
= Rio - Ruby I/O Facilitator
fa-cil-i-tate: To make easy or easier [http://
www.thefreedictionary.com/facilitate]
== Overview
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.
== SYNOPSIS
For the following assume:
astring = ""
anarray = []
Iterate over the .rb files in a directory.
rio('adir').files('*.rb') { |entrio| ... }
Return an array of the .rb files in a directory.
rio('adir').files['*.rb']
Copy the .rb files in a directory.to another directory.
rio('adir').files('*.rb') > rio('another_directory')
Iterate over the .rb files in a directory and its subdirectories.
rio('adir').all.files('*.rb') { |entrio| ... }
Return an array of the .rb files in a directory and its
subdirectories.
rio('adir').all.files['*.rb']
Copy or append a file to a string
rio('afile') > astring # copy
rio('afile') >> astring # append
Copy or append a string to a file
rio('afile') < astring # copy
rio('afile') << astring # append
Copy or append the lines of a file to an array
rio('afile') > anarray
rio('afile') >> anarray
Copy or append a file to another file
rio('afile') > rio('another_file')
rio('afile') >> rio('another_file')
Copy a file to a directory
rio('adir') << rio('afile')
Copy a directory to another directory
rio('adir') >> rio('another_directory')
Copy a web-page to a file
rio('
http://rubydoc...
) > rio('afile')
Read a web-page into a string
astring = rio('
http://rubydoc...
).read
Ways to get the chomped lines of a file into an array
anarray = rio('afile').chomp[] # subscript operator
rio('afile').chomp > anarray # copy-to operator
anarray = rio('afile').chomp.to_a # to_a
anarray = rio('afile').chomp.readlines # IO#readlines
Iterate over selected lines of a file
rio('adir').lines(0..3) { |aline| ... } # a range of lines
rio('adir').lines(/re/) { |aline| ... } # by regular expression
rio('adir').lines(0..3,/re/) { |aline| ... } # or both
Return selected lines of a file as an array
rio('adir').lines[0..3] # a range of lines
rio('adir').lines[/re/] # by regular expression
rio('adir').lines[0..3,/re/] # or both
Iterate over selected chomped lines of a file
rio('adir').chomp.lines(0..3) { |aline| ... } # a range of
lines
rio('adir').chomp.lines(/re/) { |aline| ... } # by regular
expression
Return selected chomped lines of a file as an array
rio('adir').chomp[0..3] # a range of lines
rio('adir').chomp[/re/] # by regular expression
Copy a gzipped file un-gzipping it
rio('afile.gz').gzip > rio('afile')
Copy a plain file, gzipping it
rio('afile.gz').gzip < rio('afile')
Copy a file from a ftp server into a local file un-gzipping it
rio('ftp://host/afile.gz').gzip > rio('afile')
Return an array of .rb files excluding symlinks to .rb files
rio('adir').files('*.rb').skip[:symlink?]
Put the first 10 chomped lines of a gzipped file into an array
anarray = rio('afile.gz').chomp.gzip[0...10]
Copy lines 0 and 3 thru 5 of a gzipped file on an ftp server to stdout
rio('ftp://host/afile.gz').gzip.lines(0,3..5) > ?-
Return an array of files in a directory and its subdirectories,
without descending into .svn directories.
rio('adir').norecurse(/^\.svn$/).files[]
Iterate over the non-empty, non-comment chomped lines of a file
rio('afile').chomp.skip(:empty?,/^\s*#/) { |line| ... }
Copy the output of th ps command into an array, skipping the header
line and the ps command entry
rio(?-,'ps -a').skiplines(0,/ps$/) > anarray
Prompt for input and return what was typed
ans = rio(?-).print("Type Something: ").chomp.gets
Change the extension of all .htm files in a directory and its
subdirectories to .html
rio('adir').rename.all.files('*.htm') do |htmfile|
htmfile.extname = '.html'
end
Copy a CSV file, changing the separator to a semicolon
rio('comma.csv').csv > rio('semicolon.csv').csv(';')
Iterate through a CSVfile with each line parsed into an array
rio('afile.csv').csv { |array_of_fields| ...}
Create a tab separated file of accounts in a UNIX passwd file,
listing only the username, uid, and realname fields
rio('/etc/passwd').csv(':').columns(0,2,4) > rio('rpt').csv("\t")
Pipe multiple commands
rio('afile') | rio(?-,'acmd') | 'another_cmd' | ?-
== Contact
Project::
http://rubyforge.org/pro...
Documentation::
http://rio.ruby...
Bugs::
http://rubyforge.org/tracker/?gr...
Email:: rio4ruby@rubyforge.org
== Copyright
Copyright (c) 2005,2006,2007 Christopher Kleckner. All rights reserved
== License
Rio is released under the GNU General Public License
(
http://www.gnu.org/license...
)
-Christopher Kleckner
2 Answers
Leslie Viljoen
4/3/2007 8:22:00 AM
0
Wow, thanks! I will surely make good use of this!
Leslie
Jens Wille
4/3/2007 12:04:00 PM
0
hi christopher!
rio4ruby [03/04/07 05:10]:
> Rio 0.4.0
>
> = Rio - Ruby I/O Facilitator
>
> fa-cil-i-tate: To make easy or easier [http://
> www.thefreedictionary.com/facilitate]
awesome! just went through the code and docs. awesome library,
awesome docs! ...what can i say more? ;-)
one suggestion though: what about adding a content_type method to
RIO::IF::File? this would allow to treat local and remote files
alike, just call content_type on the respective rio.
rio('
http://example.com/imag...
).content_type #=> "image/png"
rio('some/local/image.png').content_type #=> "image/png"
the former via open-uri, the latter via mime-types (which, however,
is not part of the standard library). please see attached diff for a
simple implementation.
cheers
jens
--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...
*** rio-0.4.0/lib/rio/if/file.rb~ 2007-04-03 13:36:00.000000000 +0200
--- rio-0.4.0/lib/rio/if/file.rb 2007-04-03 13:40:08.000000000 +0200
***************
*** 35 ****
--- 36 ----
+ require 'mime/types'
***************
*** 80 ****
--- 82,89 ----
+ def mime_types
+ MIME::Types.of(self.path)
+ end
+
+ def content_type
+ mime_types.first.content_type
+ end
+
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
{ANN] Rio 0.4.0
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password