ruby-nuggets 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (244) hide show
  1. checksums.yaml +4 -4
  2. metadata +20 -324
  3. data/COPYING +0 -663
  4. data/ChangeLog +0 -7
  5. data/README +0 -62
  6. data/Rakefile +0 -47
  7. data/lib/nuggets.rb +0 -73
  8. data/lib/nuggets/all.rb +0 -36
  9. data/lib/nuggets/all_mixins.rb +0 -34
  10. data/lib/nuggets/ansicolor2css.rb +0 -125
  11. data/lib/nuggets/argv/option.rb +0 -3
  12. data/lib/nuggets/argv/option_mixin.rb +0 -85
  13. data/lib/nuggets/array/boost.rb +0 -5
  14. data/lib/nuggets/array/boost_mixin.rb +0 -78
  15. data/lib/nuggets/array/combination.rb +0 -63
  16. data/lib/nuggets/array/correlation.rb +0 -5
  17. data/lib/nuggets/array/correlation_mixin.rb +0 -64
  18. data/lib/nuggets/array/flatten_once.rb +0 -57
  19. data/lib/nuggets/array/flush.rb +0 -5
  20. data/lib/nuggets/array/flush_mixin.rb +0 -47
  21. data/lib/nuggets/array/format.rb +0 -66
  22. data/lib/nuggets/array/hashify.rb +0 -5
  23. data/lib/nuggets/array/hashify_mixin.rb +0 -48
  24. data/lib/nuggets/array/histogram.rb +0 -5
  25. data/lib/nuggets/array/histogram_mixin.rb +0 -170
  26. data/lib/nuggets/array/in_order.rb +0 -51
  27. data/lib/nuggets/array/limit.rb +0 -5
  28. data/lib/nuggets/array/limit_mixin.rb +0 -57
  29. data/lib/nuggets/array/mean.rb +0 -5
  30. data/lib/nuggets/array/mean_mixin.rb +0 -203
  31. data/lib/nuggets/array/median.rb +0 -5
  32. data/lib/nuggets/array/median_mixin.rb +0 -74
  33. data/lib/nuggets/array/mode.rb +0 -5
  34. data/lib/nuggets/array/mode_mixin.rb +0 -70
  35. data/lib/nuggets/array/monotone.rb +0 -84
  36. data/lib/nuggets/array/only.rb +0 -42
  37. data/lib/nuggets/array/rand.rb +0 -45
  38. data/lib/nuggets/array/regression.rb +0 -5
  39. data/lib/nuggets/array/regression_mixin.rb +0 -150
  40. data/lib/nuggets/array/runiq.rb +0 -5
  41. data/lib/nuggets/array/runiq_mixin.rb +0 -53
  42. data/lib/nuggets/array/shuffle.rb +0 -133
  43. data/lib/nuggets/array/standard_deviation.rb +0 -5
  44. data/lib/nuggets/array/standard_deviation_mixin.rb +0 -51
  45. data/lib/nuggets/array/to_hash.rb +0 -65
  46. data/lib/nuggets/array/variance.rb +0 -5
  47. data/lib/nuggets/array/variance_mixin.rb +0 -82
  48. data/lib/nuggets/cli.rb +0 -8
  49. data/lib/nuggets/content_type.rb +0 -98
  50. data/lib/nuggets/dotted_decimal.rb +0 -60
  51. data/lib/nuggets/enumerable/agrep.rb +0 -80
  52. data/lib/nuggets/enumerable/all_any_extended.rb +0 -67
  53. data/lib/nuggets/enumerable/minmax.rb +0 -103
  54. data/lib/nuggets/env/set.rb +0 -3
  55. data/lib/nuggets/env/set_mixin.rb +0 -68
  56. data/lib/nuggets/env/user_encoding.rb +0 -3
  57. data/lib/nuggets/env/user_encoding_mixin.rb +0 -55
  58. data/lib/nuggets/env/user_home.rb +0 -3
  59. data/lib/nuggets/env/user_home_mixin.rb +0 -57
  60. data/lib/nuggets/file/ext.rb +0 -5
  61. data/lib/nuggets/file/ext_mixin.rb +0 -88
  62. data/lib/nuggets/file/replace.rb +0 -5
  63. data/lib/nuggets/file/replace_mixin.rb +0 -56
  64. data/lib/nuggets/file/sub.rb +0 -5
  65. data/lib/nuggets/file/sub_mixin.rb +0 -96
  66. data/lib/nuggets/file/which.rb +0 -5
  67. data/lib/nuggets/file/which_mixin.rb +0 -73
  68. data/lib/nuggets/hash/at.rb +0 -69
  69. data/lib/nuggets/hash/deep_fetch.rb +0 -5
  70. data/lib/nuggets/hash/deep_fetch_mixin.rb +0 -75
  71. data/lib/nuggets/hash/deep_merge.rb +0 -5
  72. data/lib/nuggets/hash/deep_merge_mixin.rb +0 -54
  73. data/lib/nuggets/hash/idmap.rb +0 -5
  74. data/lib/nuggets/hash/idmap_mixin.rb +0 -42
  75. data/lib/nuggets/hash/in_order.rb +0 -44
  76. data/lib/nuggets/hash/insert.rb +0 -54
  77. data/lib/nuggets/hash/nest.rb +0 -5
  78. data/lib/nuggets/hash/nest_mixin.rb +0 -79
  79. data/lib/nuggets/hash/only.rb +0 -53
  80. data/lib/nuggets/hash/seen.rb +0 -5
  81. data/lib/nuggets/hash/seen_mixin.rb +0 -59
  82. data/lib/nuggets/hash/unroll.rb +0 -5
  83. data/lib/nuggets/hash/unroll_mixin.rb +0 -89
  84. data/lib/nuggets/hash/zip.rb +0 -5
  85. data/lib/nuggets/hash/zip_mixin.rb +0 -160
  86. data/lib/nuggets/i18n.rb +0 -156
  87. data/lib/nuggets/integer/factorial.rb +0 -57
  88. data/lib/nuggets/integer/length.rb +0 -5
  89. data/lib/nuggets/integer/length_mixin.rb +0 -50
  90. data/lib/nuggets/integer/map.rb +0 -5
  91. data/lib/nuggets/integer/map_mixin.rb +0 -43
  92. data/lib/nuggets/integer/to_binary_s.rb +0 -39
  93. data/lib/nuggets/io/agrep.rb +0 -44
  94. data/lib/nuggets/io/interact.rb +0 -5
  95. data/lib/nuggets/io/interact_mixin.rb +0 -160
  96. data/lib/nuggets/io/modes.rb +0 -122
  97. data/lib/nuggets/io/null.rb +0 -5
  98. data/lib/nuggets/io/null_mixin.rb +0 -41
  99. data/lib/nuggets/io/redirect.rb +0 -5
  100. data/lib/nuggets/io/redirect_mixin.rb +0 -51
  101. data/lib/nuggets/lazy_attr.rb +0 -45
  102. data/lib/nuggets/log_parser.rb +0 -71
  103. data/lib/nuggets/log_parser/apache.rb +0 -102
  104. data/lib/nuggets/log_parser/rails.rb +0 -220
  105. data/lib/nuggets/lsi.rb +0 -8
  106. data/lib/nuggets/midos.rb +0 -8
  107. data/lib/nuggets/mysql.rb +0 -8
  108. data/lib/nuggets/net/success.rb +0 -60
  109. data/lib/nuggets/numeric/between.rb +0 -2
  110. data/lib/nuggets/numeric/duration.rb +0 -101
  111. data/lib/nuggets/numeric/limit.rb +0 -63
  112. data/lib/nuggets/numeric/signum.rb +0 -53
  113. data/lib/nuggets/numeric/to_multiple.rb +0 -62
  114. data/lib/nuggets/object/blank.rb +0 -20
  115. data/lib/nuggets/object/blank_mixin.rb +0 -100
  116. data/lib/nuggets/object/boolean.rb +0 -5
  117. data/lib/nuggets/object/boolean_mixin.rb +0 -62
  118. data/lib/nuggets/object/eigenclass.rb +0 -2
  119. data/lib/nuggets/object/ghost_class.rb +0 -2
  120. data/lib/nuggets/object/metaclass.rb +0 -2
  121. data/lib/nuggets/object/msend.rb +0 -5
  122. data/lib/nuggets/object/msend_mixin.rb +0 -44
  123. data/lib/nuggets/object/silence.rb +0 -5
  124. data/lib/nuggets/object/silence_mixin.rb +0 -45
  125. data/lib/nuggets/object/singleton_class.rb +0 -5
  126. data/lib/nuggets/object/singleton_class_mixin.rb +0 -96
  127. data/lib/nuggets/object/uniclass.rb +0 -2
  128. data/lib/nuggets/object/virtual_class.rb +0 -2
  129. data/lib/nuggets/pluggable.rb +0 -92
  130. data/lib/nuggets/proc/bind.rb +0 -5
  131. data/lib/nuggets/proc/bind_mixin.rb +0 -52
  132. data/lib/nuggets/range/quantile.rb +0 -5
  133. data/lib/nuggets/range/quantile_mixin.rb +0 -43
  134. data/lib/nuggets/rdf/compression.rb +0 -6
  135. data/lib/nuggets/rdf/prefix.rb +0 -6
  136. data/lib/nuggets/rdf/turtle.rb +0 -8
  137. data/lib/nuggets/rdf/turtle/reader.rb +0 -6
  138. data/lib/nuggets/rdf/uri.rb +0 -6
  139. data/lib/nuggets/ruby.rb +0 -236
  140. data/lib/nuggets/statistics.rb +0 -12
  141. data/lib/nuggets/statistics_mixins.rb +0 -12
  142. data/lib/nuggets/string/camelscore.rb +0 -5
  143. data/lib/nuggets/string/camelscore_mixin.rb +0 -117
  144. data/lib/nuggets/string/capitalize_first.rb +0 -47
  145. data/lib/nuggets/string/case.rb +0 -82
  146. data/lib/nuggets/string/evaluate.rb +0 -5
  147. data/lib/nuggets/string/evaluate_mixin.rb +0 -48
  148. data/lib/nuggets/string/msub.rb +0 -85
  149. data/lib/nuggets/string/nsub.rb +0 -66
  150. data/lib/nuggets/string/sub_with_md.rb +0 -112
  151. data/lib/nuggets/string/wc.rb +0 -5
  152. data/lib/nuggets/string/wc_mixin.rb +0 -96
  153. data/lib/nuggets/string/word_wrap.rb +0 -77
  154. data/lib/nuggets/string/xor.rb +0 -5
  155. data/lib/nuggets/string/xor_mixin.rb +0 -60
  156. data/lib/nuggets/tempfile/open.rb +0 -58
  157. data/lib/nuggets/uri/content_type.rb +0 -5
  158. data/lib/nuggets/uri/content_type_mixin.rb +0 -48
  159. data/lib/nuggets/uri/exist.rb +0 -5
  160. data/lib/nuggets/uri/exist_mixin.rb +0 -57
  161. data/lib/nuggets/uri/redirect.rb +0 -5
  162. data/lib/nuggets/uri/redirect_mixin.rb +0 -102
  163. data/lib/nuggets/util/ansicolor2css.rb +0 -4
  164. data/lib/nuggets/util/cli.rb +0 -4
  165. data/lib/nuggets/util/content_type.rb +0 -4
  166. data/lib/nuggets/util/dotted_decimal.rb +0 -3
  167. data/lib/nuggets/util/i18n.rb +0 -4
  168. data/lib/nuggets/util/lazy_attr.rb +0 -4
  169. data/lib/nuggets/util/log_parser.rb +0 -4
  170. data/lib/nuggets/util/log_parser/apache.rb +0 -4
  171. data/lib/nuggets/util/log_parser/rails.rb +0 -4
  172. data/lib/nuggets/util/midos.rb +0 -4
  173. data/lib/nuggets/util/mysql.rb +0 -4
  174. data/lib/nuggets/util/pluggable.rb +0 -4
  175. data/lib/nuggets/util/ruby.rb +0 -4
  176. data/lib/nuggets/version.rb +0 -27
  177. data/spec/nuggets/array/boost_spec.rb +0 -50
  178. data/spec/nuggets/array/combination_spec.rb +0 -25
  179. data/spec/nuggets/array/correlation_spec.rb +0 -81
  180. data/spec/nuggets/array/flatten_once_spec.rb +0 -16
  181. data/spec/nuggets/array/flush_spec.rb +0 -43
  182. data/spec/nuggets/array/format_spec.rb +0 -52
  183. data/spec/nuggets/array/hashify_spec.rb +0 -41
  184. data/spec/nuggets/array/histogram_spec.rb +0 -87
  185. data/spec/nuggets/array/in_order_spec.rb +0 -13
  186. data/spec/nuggets/array/limit_spec.rb +0 -62
  187. data/spec/nuggets/array/mean_spec.rb +0 -203
  188. data/spec/nuggets/array/median_spec.rb +0 -77
  189. data/spec/nuggets/array/mode_spec.rb +0 -57
  190. data/spec/nuggets/array/monotone_spec.rb +0 -30
  191. data/spec/nuggets/array/only_spec.rb +0 -26
  192. data/spec/nuggets/array/regression_spec.rb +0 -54
  193. data/spec/nuggets/array/runiq_spec.rb +0 -25
  194. data/spec/nuggets/array/standard_deviation_spec.rb +0 -33
  195. data/spec/nuggets/array/to_hash_spec.rb +0 -28
  196. data/spec/nuggets/array/variance_spec.rb +0 -106
  197. data/spec/nuggets/dotted_decimal_spec.rb +0 -27
  198. data/spec/nuggets/enumerable/all_any_extended_spec.rb +0 -31
  199. data/spec/nuggets/enumerable/minmax_spec.rb +0 -21
  200. data/spec/nuggets/env/set_spec.rb +0 -29
  201. data/spec/nuggets/env/user_encoding_spec.rb +0 -38
  202. data/spec/nuggets/env/user_home_spec.rb +0 -42
  203. data/spec/nuggets/file/ext_spec.rb +0 -38
  204. data/spec/nuggets/file/replace_spec.rb +0 -95
  205. data/spec/nuggets/file/sub_spec.rb +0 -149
  206. data/spec/nuggets/file/which_spec.rb +0 -22
  207. data/spec/nuggets/hash/at_spec.rb +0 -19
  208. data/spec/nuggets/hash/deep_fetch_spec.rb +0 -159
  209. data/spec/nuggets/hash/deep_merge_spec.rb +0 -78
  210. data/spec/nuggets/hash/in_order_spec.rb +0 -12
  211. data/spec/nuggets/hash/insert_spec.rb +0 -13
  212. data/spec/nuggets/hash/nest_spec.rb +0 -102
  213. data/spec/nuggets/hash/only_spec.rb +0 -29
  214. data/spec/nuggets/hash/seen_spec.rb +0 -36
  215. data/spec/nuggets/hash/unroll_spec.rb +0 -68
  216. data/spec/nuggets/i18n_spec.rb +0 -13
  217. data/spec/nuggets/integer/factorial_spec.rb +0 -10
  218. data/spec/nuggets/integer/length_spec.rb +0 -18
  219. data/spec/nuggets/integer/map_spec.rb +0 -19
  220. data/spec/nuggets/integer/to_binary_s_spec.rb +0 -19
  221. data/spec/nuggets/numeric/duration_spec.rb +0 -25
  222. data/spec/nuggets/numeric/limit_spec.rb +0 -16
  223. data/spec/nuggets/numeric/signum_spec.rb +0 -16
  224. data/spec/nuggets/numeric/to_multiple_spec.rb +0 -16
  225. data/spec/nuggets/object/blank_spec.rb +0 -34
  226. data/spec/nuggets/object/boolean_spec.rb +0 -23
  227. data/spec/nuggets/object/msend_spec.rb +0 -25
  228. data/spec/nuggets/object/silence_spec.rb +0 -36
  229. data/spec/nuggets/object/singleton_class_spec.rb +0 -51
  230. data/spec/nuggets/proc/bind_spec.rb +0 -28
  231. data/spec/nuggets/range/quantile_spec.rb +0 -33
  232. data/spec/nuggets/string/camelscore_spec.rb +0 -114
  233. data/spec/nuggets/string/capitalize_first_spec.rb +0 -13
  234. data/spec/nuggets/string/case_spec.rb +0 -31
  235. data/spec/nuggets/string/evaluate_spec.rb +0 -24
  236. data/spec/nuggets/string/msub_spec.rb +0 -20
  237. data/spec/nuggets/string/nsub_spec.rb +0 -13
  238. data/spec/nuggets/string/sub_with_md_spec.rb +0 -25
  239. data/spec/nuggets/string/wc_spec.rb +0 -73
  240. data/spec/nuggets/string/word_wrap_spec.rb +0 -81
  241. data/spec/nuggets/string/xor_spec.rb +0 -57
  242. data/spec/nuggets/uri/content_type_spec.rb +0 -42
  243. data/spec/nuggets/uri/exist_spec.rb +0 -49
  244. data/spec/spec_helper.rb +0 -36
@@ -1,5 +0,0 @@
1
- require 'nuggets/integer/length_mixin'
2
-
3
- class Integer
4
- include Nuggets::Integer::LengthMixin
5
- end
@@ -1,50 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class Integer
30
- module LengthMixin
31
-
32
- # call-seq:
33
- # int.length => anInteger
34
- #
35
- # Returns the length of _int_.
36
- def length
37
- to_s.length
38
- end
39
-
40
- # call-seq:
41
- # int.digit_count => anInteger
42
- #
43
- # Returns the digit count of _int_. [ruby-core:22383]
44
- def digit_count
45
- abs.length
46
- end
47
-
48
- end
49
- end
50
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/integer/map_mixin'
2
-
3
- class Integer
4
- include Nuggets::Integer::MapMixin
5
- end
@@ -1,43 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class Integer
30
- module MapMixin
31
-
32
- # call-seq:
33
- # int.map_positive => anInteger
34
- #
35
- # Creates a bijection from *Z* to *N*, i.e., it maps _int_ to a positive
36
- # integer.
37
- def map_positive
38
- 2 * abs + (self > 0 ? 0 : 1)
39
- end
40
-
41
- end
42
- end
43
- end
@@ -1,39 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- class Integer
29
-
30
- # call-seq:
31
- # int.to_binary_s => aString
32
- # int.to_binary_s(length) => aString
33
- #
34
- # Returns _int_ as binary number string; optionally zero-padded to +length+.
35
- def to_binary_s(length = nil)
36
- "%0#{length}d" % to_s(2)
37
- end
38
-
39
- end
@@ -1,44 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- require 'nuggets/enumerable/agrep'
29
-
30
- class IO
31
-
32
- # call-seq:
33
- # IO.agrep(fd, pattern[, distance]) -> anArray
34
- # IO.agrep(fd, pattern[, distance]) { |line| ... } -> io
35
- #
36
- def self.agrep(fd, pattern, distance = 0, &block)
37
- open(fd) { |io| io.agrep(pattern, distance, &block) }
38
- end
39
-
40
- end
41
-
42
- if $0 == __FILE__
43
- puts ::File.agrep(__FILE__, /calls/, 2)
44
- end
@@ -1,5 +0,0 @@
1
- require 'nuggets/io/interact_mixin'
2
-
3
- class IO
4
- extend Nuggets::IO::InteractMixin
5
- end
@@ -1,160 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- module Nuggets
29
- class IO
30
- module InteractMixin
31
-
32
- # call-seq:
33
- # IO.interact(input, output[, timeout[, maxlen]]) -> anArray | nil
34
- #
35
- # Interact with both ends of a pipe in a non-blocking manner.
36
- #
37
- # +input+ represents the sending end and is a mapping from the actual
38
- # input IO (to send into the pipe) to the pipe's input handle (_stdin_).
39
- # The input IO must support +read_nonblock+. If it's a StringIO it will
40
- # be extended appropriately. If it's a String it will be converted to
41
- # a StringIO.
42
- #
43
- # +output+ represents the receiving end and is a mapping from the pipe's
44
- # output handle (_stdout_) to the designated output IO (to receive data
45
- # from the pipe), and, optionally, from the pipe's error handle (_stderr_)
46
- # to the designated error IO. The output and error IO must support <tt><<</tt>
47
- # with a string argument. If either of them is a Proc it will be extended such
48
- # that <tt><<</tt> delegates to +call+.
49
- #
50
- # +timeout+, if given, will be passed to IO::select and +nil+ is returned
51
- # if the select call times out; in all other cases an empty array is returned.
52
- #
53
- # +maxlen+ is the chunk size for +read_nonblock+.
54
- #
55
- # Examples:
56
- #
57
- # require 'open3'
58
- #
59
- # # simply prints 'input string' on STDOUT, ignores +stderr+
60
- # Open3.popen3('cat') { |stdin, stdout, stderr|
61
- # IO.interact({ "input string\n" => stdin }, { stdout => STDOUT })
62
- # }
63
- #
64
- # # prints lines you type in reverse order to a string
65
- # str = ''
66
- # Open3.popen3('tac') { |stdin, stdout, stderr|
67
- # IO.interact({ STDIN => stdin }, { stdout => str })
68
- # }
69
- # puts str
70
- #
71
- # # prints the IP adresses from /etc/hosts on STDOUT and their lengths
72
- # # on STDERR
73
- # cmd = %q{ruby -ne 'i = $_.split.first or next; warn i.length; puts i'}
74
- # Open3.popen3(cmd) { |stdin, stdout, stderr|
75
- # File.open('/etc/hosts') { |f|
76
- # IO.interact({ f => stdin }, { stdout => STDOUT, stderr => STDERR })
77
- # }
78
- # }
79
- def interact(input, output, timeout = nil, maxlen = 2 ** 16)
80
- readers, writers = {}, {}
81
-
82
- output.each { |key, val|
83
- if val.is_a?(::Proc) && !val.respond_to?(:<<)
84
- class << val; alias_method :<<, :call; end
85
- end
86
-
87
- readers[key] = val
88
- }
89
-
90
- input.each { |key, val|
91
- if key.is_a?(::String)
92
- require 'stringio'
93
- key = ::StringIO.new(key)
94
- end
95
-
96
- unless key.respond_to?(:read_nonblock)
97
- def key.read_nonblock(*args)
98
- read(*args) or raise ::EOFError, 'end of string reached'
99
- end
100
- end
101
-
102
- writers[val] = [key, '']
103
- }
104
-
105
- close = lambda { |*args|
106
- container, item, read = args
107
-
108
- container.delete(item)
109
- read ? item.close_read : item.close_write
110
- }
111
-
112
- read = lambda { |*args|
113
- reader, buffer, writer = args
114
-
115
- container = writer ? writers : readers
116
- buffer ||= container[reader]
117
-
118
- begin
119
- buffer << reader.read_nonblock(maxlen)
120
- rescue ::Errno::EAGAIN
121
- rescue ::EOFError
122
- buffer.force_encoding(
123
- reader.internal_encoding ||
124
- reader.external_encoding ||
125
- Encoding.default_internal ||
126
- Encoding.default_external
127
- ) if buffer.respond_to?(:force_encoding)
128
-
129
- close[container, writer || reader, !writer]
130
- end
131
- }
132
-
133
- until readers.empty? && writers.empty?
134
- fhs = select(readers.keys, writers.keys, nil, timeout) or return
135
-
136
- fhs[0].each { |reader| read[reader] }
137
-
138
- fhs[1].each { |writer|
139
- reader, buffer = writers[writer]
140
- read[reader, buffer, writer] or next if buffer.empty?
141
-
142
- begin
143
- bytes = writer.write_nonblock(buffer)
144
- rescue ::Errno::EPIPE
145
- close[writers, writer]
146
- end
147
-
148
- if bytes
149
- buffer.force_encoding('BINARY') if buffer.respond_to?(:force_encoding)
150
- buffer.slice!(0, bytes)
151
- end
152
- }
153
- end
154
-
155
- []
156
- end
157
-
158
- end
159
- end
160
- end
@@ -1,122 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # #
4
- # A component of ruby-nuggets, some extensions to the Ruby programming #
5
- # language. #
6
- # #
7
- # Copyright (C) 2007-2011 Jens Wille #
8
- # #
9
- # Authors: #
10
- # Jens Wille <jens.wille@gmail.com> #
11
- # #
12
- # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
- # under the terms of the GNU Affero General Public License as published by #
14
- # the Free Software Foundation; either version 3 of the License, or (at your #
15
- # option) any later version. #
16
- # #
17
- # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
- # for more details. #
21
- # #
22
- # You should have received a copy of the GNU Affero General Public License #
23
- # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
- # #
25
- ###############################################################################
26
- #++
27
-
28
- class IO
29
-
30
- # Inspired by Martin DeMello, [ruby-talk:307782] -- thx ;-)
31
-
32
- class << self
33
-
34
- alias_method :_nuggets_original_read, :read
35
-
36
- # call-seq:
37
- # IO.read(name[, length[, offset]]]) => aString
38
- # IO.read(name[, binary]) { |io| ... } => anObject
39
- #
40
- # Opens +name+ with mode +r+. NOTE: With no associated block,
41
- # acts like the original IO::read, not like IO::new.
42
- def read(name, *args)
43
- return _nuggets_original_read(name, *args) unless block_given?
44
-
45
- case args.size
46
- when 0
47
- # ok
48
- when 1
49
- case binary = args.first
50
- when true, false, nil
51
- # ok
52
- else
53
- raise ::TypeError, "wrong argument type #{binary.class} (expected boolean)"
54
- end
55
- else
56
- raise ::ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
57
- end
58
-
59
- open_with_mode(name, 'r', binary, &::Proc.new)
60
- end
61
-
62
- # call-seq:
63
- # IO.write(name[, binary]) => anIO
64
- # IO.write(name[, binary]) { |io| ... } => anObject
65
- #
66
- # Opens +name+ with mode +w+.
67
- def write(name, binary = false)
68
- open_with_mode(name, 'w', binary, &block_given? ? ::Proc.new : nil)
69
- end
70
-
71
- # call-seq:
72
- # IO.append(name[, binary]) => anIO
73
- # IO.append(name[, binary]) { |io| ... } => anObject
74
- #
75
- # Opens +name+ with mode +a+.
76
- def append(name, binary = false)
77
- open_with_mode(name, 'a', binary, &block_given? ? ::Proc.new : nil)
78
- end
79
-
80
- # call-seq:
81
- # IO.read_write(name[, binary]) => anIO
82
- # IO.read_write(name[, binary]) { |io| ... } => anObject
83
- #
84
- # Opens +name+ with mode <tt>r+</tt>.
85
- def read_write(name, binary = false)
86
- open_with_mode(name, 'r+', binary, &block_given? ? ::Proc.new : nil)
87
- end
88
-
89
- # call-seq:
90
- # IO.write_read(name[, binary]) => anIO
91
- # IO.write_read(name[, binary]) { |io| ... } => anObject
92
- #
93
- # Opens +name+ with mode <tt>w+</tt>.
94
- def write_read(name, binary = false)
95
- open_with_mode(name, 'w+', binary, &block_given? ? ::Proc.new : nil)
96
- end
97
-
98
- # call-seq:
99
- # IO.append_read(name[, binary]) => anIO
100
- # IO.append_read(name[, binary]) { |io| ... } => anObject
101
- #
102
- # Opens +name+ with mode <tt>a+</tt>.
103
- def append_read(name, binary = false)
104
- open_with_mode(name, 'a+', binary, &block_given? ? ::Proc.new : nil)
105
- end
106
-
107
- private
108
-
109
- # Just a helper to DRY things up.
110
- def open_with_mode(name, mode, binary = false)
111
- open(name, "#{mode}#{'b' if binary}", &block_given? ? ::Proc.new : nil)
112
- end
113
-
114
- end
115
-
116
- end
117
-
118
- if $0 == __FILE__
119
- # ::File.read(__FILE__) { |f| ... }
120
- # ::File.write(__FILE__) { |f| ... }
121
- # ::File.append(__FILE__) { |f| ... }
122
- end