wishdev-rio 0.4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. data/COPYING +341 -0
  2. data/README +81 -0
  3. data/Rakefile +281 -0
  4. data/build_doc.rb +94 -0
  5. data/doc/ANNOUNCE +159 -0
  6. data/doc/RELEASE_NOTES +308 -0
  7. data/doc/RIOIS +215 -0
  8. data/doc/generators/template/html/rio.css +428 -0
  9. data/doc/generators/template/html/rio.rb +523 -0
  10. data/doc/generators/template/html/ugly.rb +132 -0
  11. data/doc/pkg_def.rb +60 -0
  12. data/doc/rfc1738.txt +1403 -0
  13. data/doc/rfc959.txt +3933 -0
  14. data/ex/catcsv.rb +64 -0
  15. data/ex/colx.rb +8 -0
  16. data/ex/findinruby +15 -0
  17. data/ex/findruby +14 -0
  18. data/ex/passwd_report.rb +8 -0
  19. data/ex/prompt.rb +25 -0
  20. data/ex/rgb.txt.gz +0 -0
  21. data/ex/riocat +42 -0
  22. data/ex/riogunzip +31 -0
  23. data/ex/riogzip +24 -0
  24. data/ex/rioprompt.rb +10 -0
  25. data/ex/targz2zip +17 -0
  26. data/ex/tonl +10 -0
  27. data/lib/rio/abstract_method.rb +56 -0
  28. data/lib/rio/argv.rb +56 -0
  29. data/lib/rio/arycopy.rb +43 -0
  30. data/lib/rio/assert.rb +114 -0
  31. data/lib/rio/base.rb +56 -0
  32. data/lib/rio/callstr.rb +46 -0
  33. data/lib/rio/const.rb +51 -0
  34. data/lib/rio/construct.rb +50 -0
  35. data/lib/rio/constructor.rb +258 -0
  36. data/lib/rio/context/autoclose.rb +72 -0
  37. data/lib/rio/context/copying.rb +55 -0
  38. data/lib/rio/context/cxx.rb +66 -0
  39. data/lib/rio/context/dir.rb +120 -0
  40. data/lib/rio/context/gzip.rb +50 -0
  41. data/lib/rio/context/methods.rb +182 -0
  42. data/lib/rio/context/skip.rb +66 -0
  43. data/lib/rio/context/stream.rb +229 -0
  44. data/lib/rio/context.rb +117 -0
  45. data/lib/rio/cp.rb +370 -0
  46. data/lib/rio/def.rb +53 -0
  47. data/lib/rio/dir.rb +144 -0
  48. data/lib/rio/doc/EXAMPLES.rb +299 -0
  49. data/lib/rio/doc/HOWTO.rb +737 -0
  50. data/lib/rio/doc/INDEX.rb +311 -0
  51. data/lib/rio/doc/INTRO.rb +1068 -0
  52. data/lib/rio/doc/OPTIONAL.rb +130 -0
  53. data/lib/rio/doc/SYNOPSIS.rb +183 -0
  54. data/lib/rio/doc.rb +45 -0
  55. data/lib/rio/entrysel.rb +246 -0
  56. data/lib/rio/exception/copy.rb +97 -0
  57. data/lib/rio/exception/notimplemented.rb +57 -0
  58. data/lib/rio/exception/notsupported.rb +46 -0
  59. data/lib/rio/exception/open.rb +61 -0
  60. data/lib/rio/exception/state.rb +73 -0
  61. data/lib/rio/exception.rb +41 -0
  62. data/lib/rio/ext/csv.rb +351 -0
  63. data/lib/rio/ext/if.rb +45 -0
  64. data/lib/rio/ext/mp3info.rb +80 -0
  65. data/lib/rio/ext/splitlines.rb +253 -0
  66. data/lib/rio/ext/yaml/doc.rb +133 -0
  67. data/lib/rio/ext/yaml/tie.rb +149 -0
  68. data/lib/rio/ext/yaml.rb +164 -0
  69. data/lib/rio/ext/zipfile/fs.rb +116 -0
  70. data/lib/rio/ext/zipfile/rl.rb +251 -0
  71. data/lib/rio/ext/zipfile/rootdir.rb +117 -0
  72. data/lib/rio/ext/zipfile/state.rb +161 -0
  73. data/lib/rio/ext/zipfile/wrap.rb +204 -0
  74. data/lib/rio/ext/zipfile.rb +110 -0
  75. data/lib/rio/ext.rb +138 -0
  76. data/lib/rio/factory.rb +436 -0
  77. data/lib/rio/file.rb +118 -0
  78. data/lib/rio/filter/closeoneof.rb +103 -0
  79. data/lib/rio/filter/gzip.rb +70 -0
  80. data/lib/rio/filter.rb +94 -0
  81. data/lib/rio/fs/base.rb +41 -0
  82. data/lib/rio/fs/impl.rb +122 -0
  83. data/lib/rio/fs/native.rb +75 -0
  84. data/lib/rio/fs/stream.rb +61 -0
  85. data/lib/rio/fs/url.rb +63 -0
  86. data/lib/rio/ftp/conncache.rb +101 -0
  87. data/lib/rio/ftp/dir.rb +94 -0
  88. data/lib/rio/ftp/fs.rb +180 -0
  89. data/lib/rio/ftp/ftpfile.rb +20 -0
  90. data/lib/rio/grande.rb +97 -0
  91. data/lib/rio/handle.rb +100 -0
  92. data/lib/rio/if/basic.rb +64 -0
  93. data/lib/rio/if/csv.rb +76 -0
  94. data/lib/rio/if/dir.rb +157 -0
  95. data/lib/rio/if/file.rb +89 -0
  96. data/lib/rio/if/fileordir.rb +268 -0
  97. data/lib/rio/if/grande.rb +729 -0
  98. data/lib/rio/if/grande_entry.rb +379 -0
  99. data/lib/rio/if/grande_stream.rb +693 -0
  100. data/lib/rio/if/internal.rb +125 -0
  101. data/lib/rio/if/path.rb +462 -0
  102. data/lib/rio/if/rubyio.rb +681 -0
  103. data/lib/rio/if/string.rb +83 -0
  104. data/lib/rio/if/temp.rb +45 -0
  105. data/lib/rio/if/test.rb +282 -0
  106. data/lib/rio/if/yaml.rb +206 -0
  107. data/lib/rio/if.rb +64 -0
  108. data/lib/rio/ioh.rb +162 -0
  109. data/lib/rio/iomode.rb +109 -0
  110. data/lib/rio/ios/fail.rb +106 -0
  111. data/lib/rio/ios/generic.rb +119 -0
  112. data/lib/rio/ios/mode.rb +60 -0
  113. data/lib/rio/ios/null.rb +119 -0
  114. data/lib/rio/iowrap.rb +128 -0
  115. data/lib/rio/kernel.rb +54 -0
  116. data/lib/rio/local.rb +62 -0
  117. data/lib/rio/match.rb +53 -0
  118. data/lib/rio/matchrecord.rb +283 -0
  119. data/lib/rio/no_warn.rb +49 -0
  120. data/lib/rio/nullio.rb +159 -0
  121. data/lib/rio/open3.rb +68 -0
  122. data/lib/rio/ops/construct.rb +61 -0
  123. data/lib/rio/ops/create.rb +77 -0
  124. data/lib/rio/ops/dir.rb +346 -0
  125. data/lib/rio/ops/either.rb +134 -0
  126. data/lib/rio/ops/file.rb +102 -0
  127. data/lib/rio/ops/path.rb +296 -0
  128. data/lib/rio/ops/stream/input.rb +267 -0
  129. data/lib/rio/ops/stream/output.rb +100 -0
  130. data/lib/rio/ops/stream/read.rb +86 -0
  131. data/lib/rio/ops/stream/write.rb +57 -0
  132. data/lib/rio/ops/stream.rb +87 -0
  133. data/lib/rio/ops/symlink.rb +80 -0
  134. data/lib/rio/path/reset.rb +69 -0
  135. data/lib/rio/path.rb +129 -0
  136. data/lib/rio/piper/cp.rb +80 -0
  137. data/lib/rio/piper.rb +122 -0
  138. data/lib/rio/prompt.rb +66 -0
  139. data/lib/rio/rectype.rb +88 -0
  140. data/lib/rio/rl/base.rb +118 -0
  141. data/lib/rio/rl/builder.rb +117 -0
  142. data/lib/rio/rl/chmap.rb +66 -0
  143. data/lib/rio/rl/fs2url.rb +82 -0
  144. data/lib/rio/rl/ioi.rb +78 -0
  145. data/lib/rio/rl/path.rb +110 -0
  146. data/lib/rio/rl/pathmethods.rb +116 -0
  147. data/lib/rio/rl/uri.rb +200 -0
  148. data/lib/rio/rl/withpath.rb +296 -0
  149. data/lib/rio/scheme/aryio.rb +88 -0
  150. data/lib/rio/scheme/cmdio.rb +80 -0
  151. data/lib/rio/scheme/cmdpipe.rb +118 -0
  152. data/lib/rio/scheme/fd.rb +65 -0
  153. data/lib/rio/scheme/ftp.rb +141 -0
  154. data/lib/rio/scheme/http.rb +78 -0
  155. data/lib/rio/scheme/null.rb +55 -0
  156. data/lib/rio/scheme/path.rb +98 -0
  157. data/lib/rio/scheme/stderr.rb +55 -0
  158. data/lib/rio/scheme/stdio.rb +71 -0
  159. data/lib/rio/scheme/strio.rb +87 -0
  160. data/lib/rio/scheme/sysio.rb +63 -0
  161. data/lib/rio/scheme/tcp.rb +75 -0
  162. data/lib/rio/scheme/temp.rb +200 -0
  163. data/lib/rio/state/error.rb +72 -0
  164. data/lib/rio/state.rb +242 -0
  165. data/lib/rio/stream/base.rb +54 -0
  166. data/lib/rio/stream/duplex.rb +79 -0
  167. data/lib/rio/stream/open.rb +202 -0
  168. data/lib/rio/stream.rb +181 -0
  169. data/lib/rio/symantics.rb +45 -0
  170. data/lib/rio/tempdir.rb +132 -0
  171. data/lib/rio/to_rio/all.rb +39 -0
  172. data/lib/rio/to_rio/array.rb +39 -0
  173. data/lib/rio/to_rio/io.rb +40 -0
  174. data/lib/rio/to_rio/object.rb +42 -0
  175. data/lib/rio/to_rio/string.rb +40 -0
  176. data/lib/rio/to_rio.rb +67 -0
  177. data/lib/rio/uri/file.rb +198 -0
  178. data/lib/rio/util.rb +48 -0
  179. data/lib/rio/version.rb +51 -0
  180. data/lib/rio.rb +162 -0
  181. data/setup.rb +1360 -0
  182. data/test/bin/count_lines.rb +11 -0
  183. data/test/bin/find_lines.rb +13 -0
  184. data/test/bin/list_dir.rb +14 -0
  185. data/test/ftp/all.rb +9 -0
  186. data/test/ftp/anon_copy_data.rb +36 -0
  187. data/test/ftp/anon_misc.rb +124 -0
  188. data/test/ftp/anon_read.rb +105 -0
  189. data/test/ftp/anon_special.rb +68 -0
  190. data/test/ftp/anon_write.rb +70 -0
  191. data/test/ftp/ftp2ftp.rb +51 -0
  192. data/test/ftp/initftpfiles.rb +14 -0
  193. data/test/ftp/testdef.rb +55 -0
  194. data/test/gem_runtests.rb +15 -0
  195. data/test/http/all.rb +4 -0
  196. data/test/http/copy-from-http.rb +141 -0
  197. data/test/http/uri-meta.rb +72 -0
  198. data/test/lib/temp_server.rb +46 -0
  199. data/test/runalltests.rb +17 -0
  200. data/test/runftptests.rb +14 -0
  201. data/test/runhttp.rb +11 -0
  202. data/test/runhttptests.rb +14 -0
  203. data/test/runtests.rb +52 -0
  204. data/test/tc/abs.rb +355 -0
  205. data/test/tc/all.rb +80 -0
  206. data/test/tc/base.rb +31 -0
  207. data/test/tc/base2.rb +87 -0
  208. data/test/tc/cd1.rb +113 -0
  209. data/test/tc/clearsel.rb +68 -0
  210. data/test/tc/clone.rb +208 -0
  211. data/test/tc/closeoncopy.rb +102 -0
  212. data/test/tc/closeoneof.rb +194 -0
  213. data/test/tc/cmdpipe.rb +149 -0
  214. data/test/tc/copy-dir-samevar.rb +91 -0
  215. data/test/tc/copy-from.rb +129 -0
  216. data/test/tc/copy-to.rb +91 -0
  217. data/test/tc/copy.rb +74 -0
  218. data/test/tc/copyarray.rb +188 -0
  219. data/test/tc/copydest.rb +50 -0
  220. data/test/tc/copydir.rb +166 -0
  221. data/test/tc/copydirlines.rb +121 -0
  222. data/test/tc/copylines.rb +46 -0
  223. data/test/tc/copynonex.rb +118 -0
  224. data/test/tc/copysymlink.rb +39 -0
  225. data/test/tc/create.rb +114 -0
  226. data/test/tc/csv.rb +226 -0
  227. data/test/tc/csv2.rb +138 -0
  228. data/test/tc/csv_columns.rb +37 -0
  229. data/test/tc/csvutil.rb +56 -0
  230. data/test/tc/dir.rb +76 -0
  231. data/test/tc/dir_iter.rb +383 -0
  232. data/test/tc/dirautoclose.rb +67 -0
  233. data/test/tc/dirent.rb +178 -0
  234. data/test/tc/dirss.rb +81 -0
  235. data/test/tc/each.rb +111 -0
  236. data/test/tc/each_break.rb +243 -0
  237. data/test/tc/edf.rb +81 -0
  238. data/test/tc/empty.rb +51 -0
  239. data/test/tc/emptyriodir.rb +129 -0
  240. data/test/tc/entary.rb +227 -0
  241. data/test/tc/entsel.rb +110 -0
  242. data/test/tc/eq.rb +101 -0
  243. data/test/tc/expand_path.rb +69 -0
  244. data/test/tc/ext.rb +136 -0
  245. data/test/tc/fileno.rb +94 -0
  246. data/test/tc/files_select.rb +92 -0
  247. data/test/tc/get.rb +152 -0
  248. data/test/tc/getrec.rb +137 -0
  249. data/test/tc/gzip.rb +109 -0
  250. data/test/tc/io_each_byte.rb +60 -0
  251. data/test/tc/io_read.rb +80 -0
  252. data/test/tc/iometh.rb +149 -0
  253. data/test/tc/likeio.rb +116 -0
  254. data/test/tc/line_record_row.rb +51 -0
  255. data/test/tc/lineno.rb +196 -0
  256. data/test/tc/lines.rb +66 -0
  257. data/test/tc/misc.rb +432 -0
  258. data/test/tc/nolines.rb +204 -0
  259. data/test/tc/noqae.rb +879 -0
  260. data/test/tc/null.rb +45 -0
  261. data/test/tc/once.rb +6 -0
  262. data/test/tc/overload.rb +140 -0
  263. data/test/tc/pa.rb +158 -0
  264. data/test/tc/path_parts.rb +175 -0
  265. data/test/tc/pathop.rb +60 -0
  266. data/test/tc/paths.rb +145 -0
  267. data/test/tc/pid.rb +31 -0
  268. data/test/tc/piper.rb +143 -0
  269. data/test/tc/programs_util.rb +24 -0
  270. data/test/tc/qae.rb +493 -0
  271. data/test/tc/qae_riovar.rb +499 -0
  272. data/test/tc/readline.rb +30 -0
  273. data/test/tc/records.rb +68 -0
  274. data/test/tc/rename.rb +233 -0
  275. data/test/tc/rename_assign.rb +45 -0
  276. data/test/tc/riorl.rb +181 -0
  277. data/test/tc/route.rb +51 -0
  278. data/test/tc/selnosel.rb +33 -0
  279. data/test/tc/skip.rb +89 -0
  280. data/test/tc/skiplines.rb +71 -0
  281. data/test/tc/split.rb +28 -0
  282. data/test/tc/splitlines.rb +65 -0
  283. data/test/tc/splitpath.rb +83 -0
  284. data/test/tc/sub.rb +46 -0
  285. data/test/tc/symlink.rb +176 -0
  286. data/test/tc/symlink0.rb +348 -0
  287. data/test/tc/symlink1.rb +114 -0
  288. data/test/tc/synopsis.rb +75 -0
  289. data/test/tc/temp.rb +152 -0
  290. data/test/tc/tempdir.rb +60 -0
  291. data/test/tc/tempfile.rb +66 -0
  292. data/test/tc/testcase.rb +170 -0
  293. data/test/tc/tonl.rb +37 -0
  294. data/test/tc/truncate.rb +39 -0
  295. data/test/tc/yaml.rb +275 -0
  296. metadata +387 -0
@@ -0,0 +1,737 @@
1
+ #--
2
+ # ===============================================================================
3
+ # Copyright (c) 2005,2006,2007 Christopher Kleckner
4
+ # All rights reserved
5
+ #
6
+ # This file is part of the Rio library for ruby.
7
+ #
8
+ # Rio is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # Rio is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with Rio; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ # ===============================================================================
22
+ #++
23
+ #
24
+ # To create the documentation for Rio run the command
25
+ # ruby build_doc.rb
26
+ # from the distribution directory.
27
+ #
28
+ # Suggested Reading
29
+ # * RIO::Doc::SYNOPSIS
30
+ # * RIO::Doc::INTRO
31
+ # * RIO::Doc::HOWTO
32
+ # * RIO::Doc::EXAMPLES
33
+ # * RIO::Rio
34
+ #
35
+
36
+
37
+ module RIO
38
+ module Doc #:doc:
39
+ =begin rdoc
40
+
41
+ = Rio - Ruby I/O Facilitator
42
+
43
+ Rio is a facade for most of the standard ruby classes that deal with I/O;
44
+ providing a simple, intuitive, succinct interface to the functionality
45
+ provided by IO, File, Dir, Pathname, FileUtils, Tempfile, StringIO, OpenURI
46
+ and others. Rio also provides an application level interface which allows many
47
+ common I/O idioms to be expressed succinctly.
48
+
49
+ == HOWTO...
50
+
51
+ === Read a single file
52
+ ario = rio('afile')
53
+ string = ""
54
+ array = []
55
+
56
+ * Read a file into a string.
57
+ # method 1
58
+ string = ario.contents
59
+ # method 2
60
+ ario > string
61
+
62
+ * Append a file onto a string.
63
+ # method 1
64
+ ario >> string
65
+ # method 2
66
+ string += ario.contents
67
+
68
+ * Read lines of a file into an array
69
+ # method 1
70
+ array = ario[]
71
+ # method 2
72
+ ario > array
73
+ # method 3
74
+ array = ario.to_a
75
+ # method 4
76
+ array = ario.readlines
77
+
78
+ * Append lines of a file into an array
79
+ # method 1
80
+ ario >> array
81
+ # method 2
82
+ array += ario.lines[]
83
+
84
+ * Read the first 10 lines of a file into an array
85
+ # method 1
86
+ array = ario[0...10]
87
+ # method 2
88
+ array = ario.lines[0...10]
89
+ # method 3
90
+ ario.lines(0...10) > array
91
+
92
+ * Read lines of a file into an array, with each line chomped
93
+ # method 1
94
+ array = ario.chomp[]
95
+ # method 2
96
+ array = ario.chomp.lines[]
97
+ # method 3
98
+ ario.chomp > array
99
+
100
+ * Append the first 10 lines of a file into an array, with each line chomped
101
+ # method 1
102
+ array += ario.chomp[0...10]
103
+ # method 2
104
+ array += ario.chomp.lines[0...10]
105
+ # method 3
106
+ ario.chomp.lines(0...10) >> array
107
+
108
+ * Read all lines starting with 'require' into an array, with each line chomped
109
+ # method 1
110
+ array = ario.chomp[/^\s*require/]
111
+ # method 2
112
+ array = ario.chomp.lines[/^\s*require/]
113
+ # method 3
114
+ ario.chomp.lines(/^\s*require/) > array
115
+
116
+ * Read a gzipped file into a string
117
+ # method 1
118
+ rio('afile.gz').gzip > string
119
+ # method 2
120
+ string = rio('afile.gz').gzip.contents
121
+
122
+ * Append a gzipped file into a string
123
+ # method 1
124
+ rio('afile.gz').gzip >> string
125
+ # method 2
126
+ string += rio('afile.gz').gzip.contents
127
+
128
+ * Iterate through all the lines of a file
129
+ # method 1
130
+ rio('afile').lines { |line| ... }
131
+ # method 2
132
+ rio('afile').each { |line| ... }
133
+ # method 3
134
+ rio('afile').each_line { |line| ... }
135
+
136
+
137
+ * Iterate through the lines of a gzipped file
138
+ rio('afile.gz').gzip { |line| ... }
139
+
140
+ * Iterate through all non-empty lines of a gzipped file, with each line chomped
141
+ rio('afile.gz').gzip.chomp.skiplines(:empty?) { |line| ... }
142
+
143
+ * Iterate through the first 100 lines of a file
144
+ # method 1
145
+ rio('afile').lines(0...100) { |line| ... }
146
+
147
+ * Iterate through the first line and all ruby comment lines in a gzipped file
148
+ rio('afile.rb.gz').gzip.lines(0,/^\s*#/) { |line| ... }
149
+
150
+ * Iterate through the lines of a ruby file that are neither empty nor comments, with all lines chomped
151
+ rio('afile.rb').chomp.skiplines(/^\s*#/,:empty?) { |line| ... }
152
+
153
+ * Read all the comment lines from a ruby file into an array with all lines chomped
154
+ # method 1
155
+ array = rio('afile.rb').chomp[/^\s*#/]
156
+ # method 2
157
+ array = rio('afile.rb').chomp.lines[/^\s*#/]
158
+ # method 3
159
+ rio('afile.rb').chomp.lines(/^\s*#/) > array
160
+
161
+
162
+ * Read lines of a file into an array, with each line chomped, skipping any lines longer than 1024 chars
163
+ # method 1
164
+ array = ario.chomp[proc{ |line| line.length <= 1024}]
165
+ # method 2
166
+ ario.chomp.lines(proc{ |line| line.length <= 1024}) > array
167
+ # method 3
168
+ array = ario.chomp.skiplines[proc{ |line| line.length > 1024}]
169
+ # method 4
170
+ array = ario.chomp.lines(proc{ |line| line.length <= 1024}).to_a
171
+
172
+ ---
173
+
174
+ === Write to a single file
175
+
176
+ ario = rio('afile')
177
+ string = "A String\n"
178
+ array = ["Line 0\n","Line 1\n"]
179
+
180
+ * Write a string to a file, leaving the Rio open
181
+ # method 1
182
+ ario.puts(string)
183
+ # method 2
184
+ ario.print(string)
185
+ # method 3
186
+ ario.noautoclose < string
187
+
188
+ * Write a string to a file and close the file
189
+ # method 1
190
+ rio('afile') < string
191
+ # method 2
192
+ ario.print!(string)
193
+ # method 3
194
+ ario.print(string).close
195
+
196
+ * Append a string to a file, leaving the Rio open
197
+ # method 1
198
+ ario.a.puts(string)
199
+ # method 2
200
+ ario.a.print(string)
201
+ # method 3
202
+ ario.noautoclose << string
203
+
204
+ * Append a string to a file and close the file
205
+ # method 1
206
+ rio('afile') << string
207
+ # method 2
208
+ rio('afile').a.print!(string)
209
+ # method 3
210
+ rio('afile').a.print(string).close
211
+
212
+ * Write an array to a file, leaving the Rio open
213
+ # method 1
214
+ ario = rio('afile').nocloseoncopy
215
+ ario << array
216
+ # method 2
217
+ ario.noautoclose < array
218
+
219
+ * Write an array to a file and close the file
220
+ # method 1
221
+ rio('afile') < array
222
+
223
+
224
+ ---
225
+
226
+ === Select records
227
+ ario = rio('afile')
228
+ string = ""
229
+ array = []
230
+
231
+ * Put lines one thru ten and line 100 into an array
232
+ # method 1
233
+ array = ario[0..9,99]
234
+ # method 2
235
+ array = ario.lines[0..9,99]
236
+ # method 3
237
+ ario.lines(0..9,99) > array
238
+
239
+ * Put lines one thru ten,line 100 and lines starting with 'rio4ruby' into an array
240
+ # method 1
241
+ array = ario[0..9,99,/^rio4ruby/]
242
+ # method 2
243
+ array = ario.lines[0..9,99,/^rio4ruby/]
244
+ # method 3
245
+ ario.lines(0..9,99,/^rio4ruby/) > array
246
+
247
+ * Put lines that are longer than 128 bytes into an array
248
+ # method 1
249
+ array = ario[proc{ |l| l.length > 128}]
250
+ # method 2
251
+ array = ario.lines[proc{ |l| l.length > 128}]
252
+ # method 3
253
+ array = ario.skiplines[proc{ |l| l.length <= 128}]
254
+ # method 4
255
+ array = ario.skip.lines[proc{ |l| l.length <= 128}]
256
+
257
+ * Copy all lines that do not start with 'rio4ruby' into another file
258
+ # method 1
259
+ ario.skiplines(/^rio4ruby/) > rio('another_file')
260
+ # method 2
261
+ ario.lines.skiplines(/^rio4ruby/) > rio('another_file')
262
+ # method 3
263
+ rio('another_file') < ario.skiplines(/^rio4ruby/)
264
+
265
+ * Copy the first 10 lines and lines starting with 'rio4ruby', but exclude any lines longer than 128 bytes
266
+ # method 1
267
+ ario.lines(0...10,/^rio4ruby/).skiplines(proc{ |l| l.length > 128}] > rio('another_file')
268
+ # method 2
269
+ rio('another_file') < ario.lines(0...10,/^rio4ruby/).skiplines(proc{ |l| l.length > 128})
270
+
271
+
272
+
273
+ ---
274
+
275
+ === Select directory entries
276
+ ario = rio('adir')
277
+ string = ""
278
+ array = []
279
+
280
+ * Put all entries with the extension '.txt' into an array
281
+ # method 1
282
+ array = ario['*.txt']
283
+ # method 2
284
+ array = ario[/\.txt$/]
285
+ # method 3
286
+ array = ario.entries['*.txt']
287
+
288
+ * Put all files with the extension '.txt' into an array
289
+ # method 1
290
+ array = ario.files['*.txt']
291
+ # method 2
292
+ array = ario.files[/\.txt$/]
293
+ # method 3
294
+ array = ario.files['*.txt']
295
+
296
+ * Put all entries with the extension '.txt' into an array, including those in subdirectories
297
+ # method 1
298
+ array = ario.all['*.txt']
299
+ # method 2
300
+ array = ario.all[/\.txt$/]
301
+ # method 3
302
+ array = ario.all.entries['*.txt']
303
+
304
+ * Put all entries with the extension '.txt' into an array, including those in subdirectories, except those
305
+ in subdirectories name '.svn'
306
+ # method 1
307
+ array = ario.norecurse('.svn').all['*.txt']
308
+ # method 2
309
+ array = ario.norecurse(/^\.svn$/).all[/\.txt$/]
310
+ # method 3
311
+ array = ario.norecurse('.svn').entries['*.txt']
312
+ # method 4
313
+ array = ario.entries('*.txt').norecurse('.svn').to_a
314
+ # method 5
315
+ array = ario.norecurse('.svn')['*.txt']
316
+
317
+ * Put all directories into an array
318
+ # method 1
319
+ array = ario.dirs[]
320
+ # method 2
321
+ array = ario.dirs.to_a
322
+
323
+ * Put all directories (recursively) into an array
324
+ # method 1
325
+ array = ario.all.dirs[]
326
+ # method 2
327
+ array = ario.all.dirs.to_a
328
+
329
+ * Put all entries (recursively) into an array, but limit the depth of recursion to 2
330
+ # method 1
331
+ array = ario.norecurse(3).to_a
332
+
333
+
334
+ * Iterate through ruby files in a directory and subdirectories skipping
335
+ those in the '.svn', and 'pkg' directories
336
+ # method 1
337
+ is_ruby_exe = proc{ |f| f.executable? and f[0][0] =~ /^#!.+ruby/ }
338
+ ario.norecurse('.svn','pkg').files('*.rb',is_ruby_exe) { |f| ... }
339
+ # method 2
340
+ is_ruby_exe = proc{ |f| f.executable? and f.gets =~ /^#!.+ruby/ }
341
+ ario.norecurse('.svn','pkg').files('*.rb',is_ruby_exe) { |f| ... }
342
+
343
+ * Put all files excluding those that are symlinks to files in an array
344
+ # method 1
345
+ array = ario.skipfiles[:symlink?]
346
+ # method 2
347
+ array = ario.skipfiles(:symlink?).files[]
348
+ # method 3
349
+ array = ario.skipfiles(:symlink?).to_a
350
+ # method 4
351
+ array = ario.files.skipfiles[:symlink?]
352
+
353
+ * Put all entries that are not files (or symlinks to files) into an array
354
+ # method 1
355
+ array = ario.skipfiles[]
356
+ # method 2
357
+ array = ario.skipfiles.to_a
358
+
359
+ * Put all entries that are symlinks to files into an array
360
+ # method 1
361
+ array = ario.files[proc{|f| f.file? and f.symlink?}]
362
+ # method 2
363
+ array = ario.files(proc{|f| f.file? and f.symlink?}).to_a
364
+
365
+ * Put all directories except those named '.svn' into an array
366
+ # method 1
367
+ array = ario.skipdirs['.svn']
368
+ # method 2
369
+ array = ario.skipdirs[/^\.svn$/]
370
+ # method 3
371
+ array = ario.skipdirs('.svn').to_a
372
+ # method 4
373
+ array = ario.skipdirs('.svn').dirs[]
374
+ # method 5
375
+ array = ario.skipdirs('.svn')[]
376
+
377
+
378
+ ---
379
+
380
+ === Read and writing files
381
+ ario = rio('afile')
382
+ string = ""
383
+ array = []
384
+
385
+ * Copy the contents of one file into another file
386
+ # method 1
387
+ rio('srcfile') > rio('dstfile')
388
+ # method 2
389
+ rio('dstfile') < rio('srcfile')
390
+ # method 3
391
+ rip('dstfile').print!(rio('srcfile').contents)
392
+
393
+ * Append the contents of one file to another file
394
+ # method 1
395
+ rio('srcfile') >> rio('dstfile')
396
+ # method 2
397
+ rio('dstfile') << rio('srcfile')
398
+ # method 3
399
+ rip('dstfile').a.print!(rio('srcfile').contents)
400
+
401
+ * Copy the first 10 lines of one file to another file
402
+ # method 1
403
+ rio('srcfile').lines(0...10) > rio('dstfile')
404
+ # method 2
405
+ rio('dstfile') < rio('srcfile').lines(0...10)
406
+ # method 3
407
+ rio('dstfile') < rio('srcfile').lines[0...10]
408
+
409
+ * Concatenate several files into one
410
+ # method 1
411
+ rio('dstfile') < [ rio('src1'), rio('src2'), rio('src3') ]
412
+ # method 2
413
+ rio('dstfile') < rio('src1') << rio('src2') << rio('src3')
414
+
415
+ * Copy a web page into a file
416
+ # method 1
417
+ rio('http://ruby-doc.org/') > rio('afile')
418
+ # method 2
419
+ rio('afile') < rio('http://ruby-doc.org/')
420
+ # method 3
421
+ rio('afile').print!(rio('http://ruby-doc.org/').contents)
422
+
423
+ * Append the output of the daytime server running on the localhost to a file
424
+ # method 1
425
+ rio("tcp://localhost:daytime") >> rio('afile')
426
+ # method 2
427
+ rio("tcp:",'localhost','daytime') >> rio('afile')
428
+ # method 3
429
+ rio('afile') << rio("tcp://:daytime")
430
+ # method 4
431
+ rio('afile') << rio("tcp://:13")
432
+
433
+ * Copy the first line and all lines containing 'http:' to a file
434
+ # method 1
435
+ rio('srcfile').lines(0,/http:/) > rio('dstfile')
436
+ # method 2
437
+ rio('dstfile') < rio('srcfile').lines(0,/http:/)
438
+ # method 3
439
+ rio('dstfile') < rio('srcfile').lines[0,/http:/]
440
+ # method 4
441
+
442
+ * Create a gzipped copy of a file
443
+ # method 1
444
+ rio('afile') > rio('afile.gz').gzip
445
+ # method 2
446
+ rio('afile.gz').gzip < rio('afile')
447
+ # method 3
448
+ rio('afile.gz').gzip.print!( rio('afile').contents )
449
+
450
+ * Create an ungzipped copy of a gzipped file
451
+ # method 1
452
+ rio('afile') < rio('afile.gz').gzip
453
+ # method 2
454
+ rio('afile.gz').gzip > rio('afile')
455
+ # method 3
456
+ rio('afile').print!( rio('afile.gz').gzip.contents )
457
+
458
+ * Copy the first 100 lines of gzipped file on a webserver into a local file
459
+ # method 1
460
+ rio('http://aserver/afile.gz').gzip.lines(0...100) > rio('afile')
461
+
462
+
463
+ * Create a file composed of a header from another file, the output of the 'ps' command, some text and
464
+ its creation time pulled from the daytime server running on your localhost
465
+ # method 1
466
+ rio('out') < [ rio('header'), rio(?-,'ps'), "Created on ", rio('tcp://:daytime') ]
467
+ # method 2
468
+ rio('out') < rio('header') << rio(?-,'ps') << "Created on: " << rio("tcp://:daytime")
469
+
470
+
471
+ ---
472
+
473
+ === Reading multiple files
474
+ ario = rio('adir')
475
+ string = ""
476
+ array = []
477
+
478
+ * Count the lines of code in a directory tree of ruby source files
479
+ # method 1
480
+ cnt = ario.all.files('*.rb').skiplines[/^\s*#/,/^\s*$/].size
481
+ # method 2
482
+ cnt = ario.all.files('*.rb').skiplines(/^\s*#/,/^\s*$/).inject(0) { |sum,l| sum += 1 }
483
+
484
+ * Concatanate the contents of all .txt files in a directory into an array
485
+ # method 1
486
+ array = ario.lines.files['*.txt']
487
+ # method 2
488
+ array = ario.files('*.txt').lines[]
489
+ # method 3
490
+ ario.files('*.txt').lines > array
491
+
492
+ * Concatanate the first line of all .txt files in a directory into an array
493
+ # method 1
494
+ array = ario.lines(0).files['*.txt']
495
+ # method 2
496
+ array = ario.files('*.txt').lines[0]
497
+ # method 3
498
+ ario.files('*.txt').lines(0) > array
499
+
500
+ * Copy all .txt files (but only their first ten lines) in a directory into another directiory
501
+ # method 1
502
+ ario.files('*.txt').lines(0...10) > rio('another_dir')
503
+
504
+ ---
505
+
506
+ === Read and write using Standard IO
507
+ string = ""
508
+ array = []
509
+
510
+ * Prompt for input and return what was typed
511
+ # method 1
512
+ ans = rio(?-).chomp.print("Type Something: ").gets
513
+ # method 2
514
+ stdio = rio(?-).chomp
515
+ ans = stdio.print("Type Something: ").gets
516
+
517
+ * Create a Rio tied to stdin or stdout, depending on how it is used
518
+ stdio = rio(?-)
519
+
520
+ * Create a Rio tied to stderr
521
+ stderr = rio(?=)
522
+
523
+ * Write a string to stdout
524
+ # method 1
525
+ rio(?-).puts("Hello World")
526
+ # method 2
527
+ rio(?-) << "Hello World\n"
528
+ # method 3
529
+ rio(?-) < "Hello World\n"
530
+
531
+ * Read a string from stdin with the input chomped
532
+ # method 1
533
+ ans = rio(?-).chomp.gets
534
+ # method 2
535
+ stdio = rio(?-).chomp
536
+ ans = stdio.gets
537
+
538
+ * Read from stdin until end of file with the result going into a string
539
+ # method 1
540
+ rio(?-) >> string
541
+ # method 2
542
+ rio(?-) > string
543
+
544
+ * Read from stdin until end of file with the chomped lines going into an array
545
+ # method 1
546
+ rio(?-).chomp >> array
547
+ # method 2
548
+ rio(?-).chomp > array
549
+
550
+ * Read from stdin until end of file with the result going into a file
551
+ # method 1
552
+ rio(?-) > rio('afile')
553
+ # method 2
554
+ rio('afile') < rio(?-)
555
+
556
+ * Read from stdin until end of file with the result appended to a file
557
+ # method 1
558
+ rio(?-) >> rio('afile')
559
+ # method 2
560
+ rio('afile') << rio(?-)
561
+
562
+ * Write a message to stderr
563
+ # method 1
564
+ rio(?=).puts("Hello Error")
565
+ # method 2
566
+ rio(?=) << "Hello Error\n"
567
+ # method 3
568
+ rio(?=) < "Hello Error\n"
569
+
570
+ * Dump a file to stdout
571
+ # method 1
572
+ rio('afile') > ?-
573
+ # method 2
574
+ rio('afile') > rio(?-)
575
+ # method 3
576
+ rio(?-) << rio('afile')
577
+ # method 4
578
+ rio('afile') >> ?-
579
+ # method 5
580
+ rio(?-) < rio('afile')
581
+ # method 6
582
+ rio(?-).print(rio('afile').contents)
583
+
584
+ * Emulate a simplified unix 'head' command which reads from stdin and writes the first 10 lines to stdout
585
+ # method 1
586
+ rio(?-).lines(0..9) > ?-
587
+
588
+ ---
589
+
590
+ === Reading and writing from processes as one might do with popen
591
+
592
+ * Read the output of the 'ps' command into an array without the header line or the line representing
593
+ the 'ps' command itself
594
+ ps = rio(?-,'ps -a').skiplines[0,/ps$/]
595
+
596
+ * Run an external program, copying its input from one location and its output to another,
597
+ and make it look very much like a shell command.
598
+
599
+ infile = rio(?","Hello Kitty\n")
600
+ outfile = rio('out.txt')
601
+
602
+ # method 1
603
+ cat = rio(?-,'cat').w!
604
+ cat <infile >outfile
605
+
606
+ # method 2
607
+ infile | 'cat' | outfile
608
+
609
+ ---
610
+
611
+ === Renaming and moving files
612
+ string = ""
613
+ array = []
614
+
615
+ * Rename the file 'a' to 'b'
616
+ # method 1
617
+ rio('a').rename('b')
618
+ # method 2
619
+ rio('a').rename.filename = 'b'
620
+
621
+ * Rename a file and leave the Rio referencing the files old name
622
+ ario = rio('a')
623
+ # method 1
624
+ ario.rename('b')
625
+
626
+ * Rename a file and change the Rio to reference the new file name
627
+ ario = rio('a')
628
+ # method 1
629
+ ario.rename!('b')
630
+
631
+ * Rename the file 'index.htm' to 'index.html'
632
+ # method 1
633
+ rio('index.htm').rename('index.html')
634
+ # method 2
635
+ rio('index.htm').rename.extname = '.html'
636
+
637
+ * Rename the file 'index.html' to 'welcome.html'
638
+ # method 1
639
+ rio('index.html').rename('welecome.html')
640
+ # method 2
641
+ rio('index.htm').rename.basename = 'welcome'
642
+
643
+ * Move a file from directory 'src' to directory 'dst'
644
+ # method 1
645
+ rio('src/afile').rename('dst/afile')
646
+ # method 2
647
+ rio('src/afile').rename.dirname = 'dst'
648
+
649
+ * Change a file to have the extension '.html' leaving the rest of it as is
650
+ # method 1
651
+ ario.rename.extname = '.html'
652
+
653
+ * Change a files basename to 'rio4ruby' without changing its extension
654
+ # method 1
655
+ ario.rename.basename = 'rio4ruby'
656
+
657
+ * Change a file ending with '.tar.gz' to end with '.tgz'
658
+ # method 1
659
+ ario.rename.ext('.tar.gz').extname = '.tgz'
660
+
661
+ * Change the extension of all files with the extension '.htm' in a directory to have the
662
+ extension '.html'
663
+ # method 1
664
+ rio('adir').rename.files('*.htm') do |htmfile|
665
+ htmlfile.extname = '.html'
666
+ end
667
+ # method 2
668
+ rio('adir').files('*.htm') do |htmfile|
669
+ htmlfile.rename.extname = '.html'
670
+ end
671
+
672
+ * Change the extension of all files with the extension '.htm' in a directory and its
673
+ subdirectories to have the extension '.html'
674
+ # method 1
675
+ rio('adir').rename.all.files('*.htm') do |htmfile|
676
+ htmfile.extname = '.html'
677
+ end
678
+ # method 2
679
+ rio('adir').all.files('*.htm') do |htmfile|
680
+ htmfile.rename.extname = '.html'
681
+ end
682
+
683
+ * Move a file in an arbitrary directory into the current working directory.
684
+ # method 1
685
+ rio('arb/i/trary/di/rec/tory/afile').rename.dirname = '.'
686
+
687
+
688
+ ---
689
+
690
+ === Manipulate a Rio's path
691
+ string = ""
692
+ array = []
693
+
694
+ * Create a Rio with an additional subdirectory appended
695
+ ap = rio('adir')
696
+ # method 1
697
+ ap /= 'subdirectory'
698
+ # method 2
699
+ ap = ap.join('subdirectory')
700
+ # method 3
701
+ ap = rio(ap,'subdirectory')
702
+
703
+ * Create a Rio from an array of subdirectories
704
+ dirs = ['adir','subdir1','subdir2']
705
+ # method 1
706
+ ario = rio(dirs)
707
+
708
+ * Create an array of subdirectories from a Rio
709
+ # method 1
710
+ anarray = rio('adir/subdir1/subdir2').split
711
+
712
+ * Append a string to a path
713
+ # method 1
714
+ ario = rio('apath') + astring
715
+ # method 2
716
+ ario = rio('apath')
717
+ ario += astring
718
+
719
+ * create a directory 'links' with a symlink pointing to each .rb file in directory 'lib' (including subdirectories)
720
+ lib = rio('lib')
721
+ links = rio('links').delete!.mkdir
722
+ lib.all.files("*.rb") do |f|
723
+ f.symlink( f.dirname.sub(/^#{lib}/,links).mkdir )
724
+ end
725
+
726
+ ----
727
+
728
+ Suggested Reading
729
+ * RIO::Doc::SYNOPSIS
730
+ * RIO::Doc::INTRO
731
+ * RIO::Rio
732
+
733
+ =end
734
+ module HOWTO
735
+ end
736
+ end
737
+ end