nuggets 0.9.9 → 1.0.0

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 (222) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +663 -0
  3. data/ChangeLog +7 -0
  4. data/README +74 -0
  5. data/Rakefile +50 -0
  6. data/lib/nuggets.rb +73 -0
  7. data/lib/nuggets/all.rb +35 -0
  8. data/lib/nuggets/all_mixins.rb +33 -0
  9. data/lib/nuggets/ansicolor2css.rb +124 -0
  10. data/lib/nuggets/argv/option.rb +3 -0
  11. data/lib/nuggets/argv/option_mixin.rb +84 -0
  12. data/lib/nuggets/array/boost.rb +5 -0
  13. data/lib/nuggets/array/boost_mixin.rb +77 -0
  14. data/lib/nuggets/array/combination.rb +62 -0
  15. data/lib/nuggets/array/correlation.rb +5 -0
  16. data/lib/nuggets/array/correlation_mixin.rb +63 -0
  17. data/lib/nuggets/array/flatten_once.rb +56 -0
  18. data/lib/nuggets/array/flush.rb +5 -0
  19. data/lib/nuggets/array/flush_mixin.rb +46 -0
  20. data/lib/nuggets/array/format.rb +65 -0
  21. data/lib/nuggets/array/hashify.rb +5 -0
  22. data/lib/nuggets/array/hashify_mixin.rb +47 -0
  23. data/lib/nuggets/array/histogram.rb +5 -0
  24. data/lib/nuggets/array/histogram_mixin.rb +169 -0
  25. data/lib/nuggets/array/in_order.rb +50 -0
  26. data/lib/nuggets/array/limit.rb +5 -0
  27. data/lib/nuggets/array/limit_mixin.rb +56 -0
  28. data/lib/nuggets/array/mean.rb +5 -0
  29. data/lib/nuggets/array/mean_mixin.rb +202 -0
  30. data/lib/nuggets/array/median.rb +5 -0
  31. data/lib/nuggets/array/median_mixin.rb +73 -0
  32. data/lib/nuggets/array/mode.rb +5 -0
  33. data/lib/nuggets/array/mode_mixin.rb +69 -0
  34. data/lib/nuggets/array/monotone.rb +83 -0
  35. data/lib/nuggets/array/only.rb +41 -0
  36. data/lib/nuggets/array/rand.rb +44 -0
  37. data/lib/nuggets/array/regression.rb +5 -0
  38. data/lib/nuggets/array/regression_mixin.rb +149 -0
  39. data/lib/nuggets/array/runiq.rb +5 -0
  40. data/lib/nuggets/array/runiq_mixin.rb +52 -0
  41. data/lib/nuggets/array/shuffle.rb +132 -0
  42. data/lib/nuggets/array/standard_deviation.rb +5 -0
  43. data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
  44. data/lib/nuggets/array/to_hash.rb +64 -0
  45. data/lib/nuggets/array/variance.rb +5 -0
  46. data/lib/nuggets/array/variance_mixin.rb +81 -0
  47. data/lib/nuggets/content_type.rb +97 -0
  48. data/lib/nuggets/dotted_decimal.rb +59 -0
  49. data/lib/nuggets/enumerable/agrep.rb +79 -0
  50. data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
  51. data/lib/nuggets/enumerable/minmax.rb +102 -0
  52. data/lib/nuggets/env/set.rb +3 -0
  53. data/lib/nuggets/env/set_mixin.rb +67 -0
  54. data/lib/nuggets/env/user_encoding.rb +3 -0
  55. data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
  56. data/lib/nuggets/env/user_home.rb +3 -0
  57. data/lib/nuggets/env/user_home_mixin.rb +56 -0
  58. data/lib/nuggets/file/ext.rb +5 -0
  59. data/lib/nuggets/file/ext_mixin.rb +87 -0
  60. data/lib/nuggets/file/replace.rb +5 -0
  61. data/lib/nuggets/file/replace_mixin.rb +55 -0
  62. data/lib/nuggets/file/sub.rb +5 -0
  63. data/lib/nuggets/file/sub_mixin.rb +95 -0
  64. data/lib/nuggets/file/which.rb +5 -0
  65. data/lib/nuggets/file/which_mixin.rb +72 -0
  66. data/lib/nuggets/hash/at.rb +68 -0
  67. data/lib/nuggets/hash/deep_fetch.rb +5 -0
  68. data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
  69. data/lib/nuggets/hash/deep_merge.rb +5 -0
  70. data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
  71. data/lib/nuggets/hash/idmap.rb +5 -0
  72. data/lib/nuggets/hash/idmap_mixin.rb +41 -0
  73. data/lib/nuggets/hash/in_order.rb +43 -0
  74. data/lib/nuggets/hash/insert.rb +53 -0
  75. data/lib/nuggets/hash/nest.rb +5 -0
  76. data/lib/nuggets/hash/nest_mixin.rb +78 -0
  77. data/lib/nuggets/hash/only.rb +52 -0
  78. data/lib/nuggets/hash/seen.rb +5 -0
  79. data/lib/nuggets/hash/seen_mixin.rb +58 -0
  80. data/lib/nuggets/hash/unroll.rb +5 -0
  81. data/lib/nuggets/hash/unroll_mixin.rb +88 -0
  82. data/lib/nuggets/hash/zip.rb +5 -0
  83. data/lib/nuggets/hash/zip_mixin.rb +159 -0
  84. data/lib/nuggets/i18n.rb +155 -0
  85. data/lib/nuggets/integer/factorial.rb +56 -0
  86. data/lib/nuggets/integer/length.rb +5 -0
  87. data/lib/nuggets/integer/length_mixin.rb +49 -0
  88. data/lib/nuggets/integer/map.rb +5 -0
  89. data/lib/nuggets/integer/map_mixin.rb +42 -0
  90. data/lib/nuggets/integer/to_binary_s.rb +38 -0
  91. data/lib/nuggets/io/agrep.rb +43 -0
  92. data/lib/nuggets/io/interact.rb +5 -0
  93. data/lib/nuggets/io/interact_mixin.rb +159 -0
  94. data/lib/nuggets/io/modes.rb +121 -0
  95. data/lib/nuggets/io/null.rb +5 -0
  96. data/lib/nuggets/io/null_mixin.rb +40 -0
  97. data/lib/nuggets/io/redirect.rb +5 -0
  98. data/lib/nuggets/io/redirect_mixin.rb +50 -0
  99. data/lib/nuggets/lazy_attr.rb +44 -0
  100. data/lib/nuggets/log_parser.rb +70 -0
  101. data/lib/nuggets/log_parser/apache.rb +101 -0
  102. data/lib/nuggets/log_parser/rails.rb +219 -0
  103. data/lib/nuggets/net/success.rb +59 -0
  104. data/lib/nuggets/numeric/between.rb +2 -0
  105. data/lib/nuggets/numeric/duration.rb +100 -0
  106. data/lib/nuggets/numeric/limit.rb +62 -0
  107. data/lib/nuggets/numeric/signum.rb +52 -0
  108. data/lib/nuggets/numeric/to_multiple.rb +61 -0
  109. data/lib/nuggets/object/blank.rb +20 -0
  110. data/lib/nuggets/object/blank_mixin.rb +99 -0
  111. data/lib/nuggets/object/boolean.rb +5 -0
  112. data/lib/nuggets/object/boolean_mixin.rb +61 -0
  113. data/lib/nuggets/object/eigenclass.rb +2 -0
  114. data/lib/nuggets/object/ghost_class.rb +2 -0
  115. data/lib/nuggets/object/metaclass.rb +2 -0
  116. data/lib/nuggets/object/msend.rb +5 -0
  117. data/lib/nuggets/object/msend_mixin.rb +43 -0
  118. data/lib/nuggets/object/silence.rb +5 -0
  119. data/lib/nuggets/object/silence_mixin.rb +44 -0
  120. data/lib/nuggets/object/singleton_class.rb +5 -0
  121. data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
  122. data/lib/nuggets/object/uniclass.rb +2 -0
  123. data/lib/nuggets/object/virtual_class.rb +2 -0
  124. data/lib/nuggets/pluggable.rb +91 -0
  125. data/lib/nuggets/proc/bind.rb +5 -0
  126. data/lib/nuggets/proc/bind_mixin.rb +51 -0
  127. data/lib/nuggets/range/quantile.rb +5 -0
  128. data/lib/nuggets/range/quantile_mixin.rb +42 -0
  129. data/lib/nuggets/ruby.rb +235 -0
  130. data/lib/nuggets/statistics.rb +12 -0
  131. data/lib/nuggets/statistics_mixins.rb +12 -0
  132. data/lib/nuggets/string/camelscore.rb +5 -0
  133. data/lib/nuggets/string/camelscore_mixin.rb +116 -0
  134. data/lib/nuggets/string/capitalize_first.rb +46 -0
  135. data/lib/nuggets/string/case.rb +81 -0
  136. data/lib/nuggets/string/evaluate.rb +5 -0
  137. data/lib/nuggets/string/evaluate_mixin.rb +47 -0
  138. data/lib/nuggets/string/msub.rb +84 -0
  139. data/lib/nuggets/string/nsub.rb +65 -0
  140. data/lib/nuggets/string/sub_with_md.rb +111 -0
  141. data/lib/nuggets/string/wc.rb +5 -0
  142. data/lib/nuggets/string/wc_mixin.rb +95 -0
  143. data/lib/nuggets/string/word_wrap.rb +76 -0
  144. data/lib/nuggets/string/xor.rb +5 -0
  145. data/lib/nuggets/string/xor_mixin.rb +59 -0
  146. data/lib/nuggets/tempfile/open.rb +57 -0
  147. data/lib/nuggets/uri/content_type.rb +5 -0
  148. data/lib/nuggets/uri/content_type_mixin.rb +47 -0
  149. data/lib/nuggets/uri/exist.rb +5 -0
  150. data/lib/nuggets/uri/exist_mixin.rb +56 -0
  151. data/lib/nuggets/uri/redirect.rb +5 -0
  152. data/lib/nuggets/uri/redirect_mixin.rb +101 -0
  153. data/lib/nuggets/version.rb +27 -0
  154. data/spec/nuggets/array/boost_spec.rb +50 -0
  155. data/spec/nuggets/array/combination_spec.rb +25 -0
  156. data/spec/nuggets/array/correlation_spec.rb +81 -0
  157. data/spec/nuggets/array/flatten_once_spec.rb +16 -0
  158. data/spec/nuggets/array/flush_spec.rb +43 -0
  159. data/spec/nuggets/array/format_spec.rb +52 -0
  160. data/spec/nuggets/array/hashify_spec.rb +41 -0
  161. data/spec/nuggets/array/histogram_spec.rb +87 -0
  162. data/spec/nuggets/array/in_order_spec.rb +13 -0
  163. data/spec/nuggets/array/limit_spec.rb +62 -0
  164. data/spec/nuggets/array/mean_spec.rb +203 -0
  165. data/spec/nuggets/array/median_spec.rb +77 -0
  166. data/spec/nuggets/array/mode_spec.rb +57 -0
  167. data/spec/nuggets/array/monotone_spec.rb +30 -0
  168. data/spec/nuggets/array/only_spec.rb +26 -0
  169. data/spec/nuggets/array/regression_spec.rb +54 -0
  170. data/spec/nuggets/array/runiq_spec.rb +25 -0
  171. data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
  172. data/spec/nuggets/array/to_hash_spec.rb +28 -0
  173. data/spec/nuggets/array/variance_spec.rb +106 -0
  174. data/spec/nuggets/dotted_decimal_spec.rb +27 -0
  175. data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
  176. data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
  177. data/spec/nuggets/env/set_spec.rb +29 -0
  178. data/spec/nuggets/env/user_encoding_spec.rb +38 -0
  179. data/spec/nuggets/env/user_home_spec.rb +42 -0
  180. data/spec/nuggets/file/ext_spec.rb +38 -0
  181. data/spec/nuggets/file/replace_spec.rb +95 -0
  182. data/spec/nuggets/file/sub_spec.rb +149 -0
  183. data/spec/nuggets/file/which_spec.rb +22 -0
  184. data/spec/nuggets/hash/at_spec.rb +19 -0
  185. data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
  186. data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
  187. data/spec/nuggets/hash/in_order_spec.rb +12 -0
  188. data/spec/nuggets/hash/insert_spec.rb +13 -0
  189. data/spec/nuggets/hash/nest_spec.rb +102 -0
  190. data/spec/nuggets/hash/only_spec.rb +29 -0
  191. data/spec/nuggets/hash/seen_spec.rb +36 -0
  192. data/spec/nuggets/hash/unroll_spec.rb +68 -0
  193. data/spec/nuggets/i18n_spec.rb +13 -0
  194. data/spec/nuggets/integer/factorial_spec.rb +10 -0
  195. data/spec/nuggets/integer/length_spec.rb +18 -0
  196. data/spec/nuggets/integer/map_spec.rb +19 -0
  197. data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
  198. data/spec/nuggets/numeric/duration_spec.rb +25 -0
  199. data/spec/nuggets/numeric/limit_spec.rb +16 -0
  200. data/spec/nuggets/numeric/signum_spec.rb +16 -0
  201. data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
  202. data/spec/nuggets/object/blank_spec.rb +34 -0
  203. data/spec/nuggets/object/boolean_spec.rb +23 -0
  204. data/spec/nuggets/object/msend_spec.rb +25 -0
  205. data/spec/nuggets/object/silence_spec.rb +36 -0
  206. data/spec/nuggets/object/singleton_class_spec.rb +51 -0
  207. data/spec/nuggets/proc/bind_spec.rb +28 -0
  208. data/spec/nuggets/range/quantile_spec.rb +33 -0
  209. data/spec/nuggets/string/camelscore_spec.rb +114 -0
  210. data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
  211. data/spec/nuggets/string/case_spec.rb +31 -0
  212. data/spec/nuggets/string/evaluate_spec.rb +24 -0
  213. data/spec/nuggets/string/msub_spec.rb +20 -0
  214. data/spec/nuggets/string/nsub_spec.rb +13 -0
  215. data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
  216. data/spec/nuggets/string/wc_spec.rb +73 -0
  217. data/spec/nuggets/string/word_wrap_spec.rb +81 -0
  218. data/spec/nuggets/string/xor_spec.rb +57 -0
  219. data/spec/nuggets/uri/content_type_spec.rb +42 -0
  220. data/spec/nuggets/uri/exist_spec.rb +49 -0
  221. data/spec/spec_helper.rb +36 -0
  222. metadata +309 -17
@@ -0,0 +1,81 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ class String
28
+
29
+ module Case
30
+
31
+ LOWER = :lower
32
+ UPPER = :upper
33
+ MIXED = :mixed
34
+
35
+ end
36
+
37
+ # call-seq:
38
+ # str.case => aSymbol
39
+ #
40
+ # Returns a symbol indicating the case of _str_.
41
+ def case
42
+ self == downcase ? Case::LOWER :
43
+ self == upcase ? Case::UPPER :
44
+ Case::MIXED
45
+ end
46
+
47
+ # call-seq:
48
+ # str.lower_case? => +true+ or +false+
49
+ #
50
+ # Tell whether _str_ is all lower case.
51
+ def lower_case?
52
+ self.case == Case::LOWER
53
+ end
54
+ alias_method :downcase?, :lower_case?
55
+
56
+ # call-seq:
57
+ # str.upper_case? => +true+ or +false+
58
+ #
59
+ # Tell whether _str_ is all upper case.
60
+ def upper_case?
61
+ self.case == Case::UPPER
62
+ end
63
+ alias_method :upcase?, :upper_case?
64
+
65
+ # call-seq:
66
+ # str.mixed_case? => +true+ or +false+
67
+ #
68
+ # Tell whether _str_ is mixed case.
69
+ def mixed_case?
70
+ self.case == Case::MIXED
71
+ end
72
+
73
+ # call-seq:
74
+ # str.capitalized? => +true+ or +false+
75
+ #
76
+ # Tell whether _str_ is capitalized.
77
+ def capitalized?
78
+ self == capitalize
79
+ end
80
+
81
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/string/evaluate_mixin'
2
+
3
+ class String
4
+ include Nuggets::String::EvaluateMixin
5
+ end
@@ -0,0 +1,47 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class String
29
+ module EvaluateMixin
30
+
31
+ # call-seq:
32
+ # str.evaluate([binding, [filename, [lineno]]]) => new_str
33
+ #
34
+ # Basically turns Kernel#eval into an instance method of String -- inspired
35
+ # by Ruby Cookbook example 1.3. This allows to pre-populate strings with
36
+ # substitution expressions <tt>"#{...}"</tt> that can get evaluated in a
37
+ # different environment (= +binding+) at a later point.
38
+ #
39
+ # Passes optional arguments +filename+ and +lineno+ on to Kernel#eval.
40
+ def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
41
+ buffer = gsub(/\\*"/) { |m| "#{"\\" * m.length}#{m}" }
42
+ eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,84 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ require 'nuggets/string/evaluate'
28
+
29
+ class String
30
+
31
+ # call-seq:
32
+ # str.msub(*substitutions) => new_str
33
+ #
34
+ # Performs _multiple_ substitutions on _str_ with order being taken into
35
+ # account (thus results of previous substitutions won't be subject to later
36
+ # ones) -- inspired by Ruby Cookbook example 1.18.
37
+ #
38
+ # The +substitutions+ parameter can be an array or a list of <tt>[pattern,
39
+ # substitution]</tt> pairs, or, simply, a hash. Note that, when using a hash,
40
+ # the ordering of how substitutions are processed might differ from what you
41
+ # intended -- instead use an array when order matters. +pattern+ can be a
42
+ # string or a regexp, +substitution+ can be a string (which may contain string
43
+ # expressions; cf. #evaluate), a proc (which will be call()ed), or any object
44
+ # really (which will be converted into a string).
45
+ def msub(*substitutions)
46
+ (_dup = dup).msub!(*substitutions) || _dup
47
+ end
48
+
49
+ # call-seq:
50
+ # str.msub!(*substitutions) => str or +nil+
51
+ #
52
+ # Destructive version of #msub.
53
+ def msub!(*substitutions)
54
+ options = substitutions.last.is_a?(::Hash) ? substitutions.pop : {}
55
+ binding = options.delete(:__binding__) || ::Kernel.binding
56
+
57
+ keys, subs, cache = [], [], {}
58
+
59
+ substitutions.concat(options.to_a).each { |key, value|
60
+ key = ::Regexp.new(::Regexp.escape(key)) unless key.is_a?(::Regexp)
61
+
62
+ keys << key
63
+ subs << [key, value]
64
+ }
65
+
66
+ gsub!(::Regexp.union(*keys)) { |match|
67
+ cache[match] ||= begin
68
+ value = subs.find { |key, _| key =~ match }.last
69
+
70
+ if value.respond_to?(:evaluate)
71
+ # make match available for string evaluation
72
+ eval("__match__ = #{match.inspect}", binding)
73
+
74
+ value.evaluate(binding)
75
+ elsif value.respond_to?(:call)
76
+ value.call(match)
77
+ else
78
+ value
79
+ end
80
+ end
81
+ }
82
+ end
83
+
84
+ end
@@ -0,0 +1,65 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ class String
28
+
29
+ # call-seq:
30
+ # str.nsub(pattern, replacement, count) => new_str
31
+ # str.nsub(pattern, count) { |match| ... } => new_str
32
+ #
33
+ # Returns a copy of _str_ with the _first_ +count+ occurrences of pattern
34
+ # replaced with either +replacement+ or the value of the block.
35
+ def nsub(*args, &block)
36
+ (_dup = dup).nsub!(*args, &block) || _dup
37
+ end
38
+
39
+ # call-seq:
40
+ # str.nsub!(pattern, replacement, count) => str or +nil+
41
+ # str.nsub!(pattern, count) { |match| ... } => str or +nil+
42
+ #
43
+ # Performs the substitutions of #nsub in place, returning _str_, or +nil+ if
44
+ # no substitutions were performed.
45
+ def nsub!(*args)
46
+ pattern, i = args.first, 0
47
+
48
+ case args.size
49
+ when 2
50
+ # Only +count+ given
51
+ count = args.last
52
+
53
+ gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match }
54
+ when 3
55
+ # Both +replacement+ and +count+ given;
56
+ # ignore block (just like String#gsub does)
57
+ replacement, count = args.values_at(1, 2)
58
+
59
+ gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
60
+ else
61
+ raise ::ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,111 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ class String
28
+
29
+ alias_method :sub_without_md, :sub
30
+ alias_method :sub_without_md!, :sub!
31
+ alias_method :gsub_without_md, :gsub
32
+ alias_method :gsub_without_md!, :gsub!
33
+
34
+ # call-seq:
35
+ # str.sub_with_md(pattern) { |match_data| ... } => new_str
36
+ #
37
+ # Just like #sub, but passes the MatchData object instead of the current
38
+ # match string to the block.
39
+ def sub_with_md(pattern, replacement = nil, &block)
40
+ replacement ?
41
+ sub_without_md(pattern, replacement) :
42
+ (_dup = dup).sub_with_md!(pattern, &block) || _dup
43
+ end
44
+
45
+ # call-seq:
46
+ # str.sub_with_md!(pattern) { |match_data| ... } => str or +nil+
47
+ #
48
+ # Destructive version of #sub_with_md.
49
+ def sub_with_md!(pattern, replacement = nil)
50
+ replacement ?
51
+ sub_without_md!(pattern, replacement) :
52
+ sub_without_md!(pattern) { |match| yield $~ }
53
+ end
54
+
55
+ # call-seq:
56
+ # str.gsub_with_md(pattern) { |match_data| ... } => new_str
57
+ #
58
+ # Just like #gsub, but passes the MatchData object instead of the current
59
+ # match string to the block.
60
+ def gsub_with_md(pattern, replacement = nil, &block)
61
+ replacement ?
62
+ gsub_without_md(pattern, replacement) :
63
+ (_dup = dup).gsub_with_md!(pattern, &block) || _dup
64
+ end
65
+
66
+ # call-seq:
67
+ # str.gsub_with_md!(pattern) { |match_data| ... } => str or +nil+
68
+ #
69
+ # Destructive version of #gsub_with_md.
70
+ def gsub_with_md!(pattern, replacement = nil)
71
+ replacement ?
72
+ gsub_without_md!(pattern, replacement) :
73
+ gsub_without_md!(pattern) { |match| yield $~ }
74
+ end
75
+
76
+ # call-seq:
77
+ # gimme_match_data!
78
+ #
79
+ # Replaces the traditional substitution methods with their MatchData passing
80
+ # equivalents. USE WITH CAUTION!
81
+ def self.gimme_match_data!
82
+ alias_method :sub, :sub_with_md
83
+ alias_method :sub!, :sub_with_md!
84
+ alias_method :gsub, :gsub_with_md
85
+ alias_method :gsub!, :gsub_with_md!
86
+ end
87
+
88
+ end
89
+
90
+ if $0 == __FILE__
91
+ s = 'Foo, Bar - Baz'
92
+ p s
93
+
94
+ String.gimme_match_data!
95
+
96
+ p s.gsub(/\w(\w+)(\W*)/) { |m|
97
+ begin
98
+ "#{$1.gsub(/[ao]/, 'X')}#{$2}"
99
+ rescue NoMethodError => err
100
+ warn err
101
+ end
102
+ }
103
+
104
+ p s.gsub(/\w(\w+)(\W*)/) { |md|
105
+ "#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
106
+ }
107
+
108
+ p s.gsub(/\w(\w+)(\W*)/) { |md|
109
+ "#{md[1].gsub(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
110
+ }
111
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/string/wc_mixin'
2
+
3
+ class String
4
+ include Nuggets::String::WcMixin
5
+ end
@@ -0,0 +1,95 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class String
29
+ module WcMixin
30
+
31
+ # call-seq:
32
+ # str.wc => anArray
33
+ #
34
+ # Count number of lines, words, and bytes in _str_.
35
+ def wc
36
+ [wc_l, wc_w, wc_c]
37
+ end
38
+
39
+ # call-seq:
40
+ # str.line_count => anInteger
41
+ #
42
+ # Count number of lines in _str_.
43
+ def line_count
44
+ count_by_re(/#{$/}/)
45
+ end
46
+ alias_method :wc_l, :line_count
47
+
48
+ # call-seq:
49
+ # str.word_count => anInteger
50
+ #
51
+ # Count number of words in _str_.
52
+ def word_count
53
+ count_by_re(/\S+/)
54
+ end
55
+ alias_method :wc_w, :word_count
56
+
57
+ # call-seq:
58
+ # str.byte_count => anInteger
59
+ #
60
+ # Count number of bytes in _str_.
61
+ def byte_count
62
+ respond_to?(:bytesize) ? bytesize : count_by_re(//n) - 1
63
+ end
64
+ alias_method :wc_c, :byte_count
65
+
66
+ # call-seq:
67
+ # str.char_count => anInteger
68
+ #
69
+ # Count number of characters in _str_.
70
+ def char_count
71
+ count_by_re(/./um)
72
+ end
73
+ alias_method :wc_m, :char_count
74
+
75
+ # call-seq:
76
+ # str.count_by_re(re) => anInteger
77
+ #
78
+ # Count number of occurrences of +re+ in _str_.
79
+ def count_by_re(re)
80
+ scan(re).size
81
+ end
82
+
83
+ # call-seq:
84
+ # str.count_by_re2(re) => anInteger
85
+ #
86
+ # A more memory-efficient version of #count_by_re.
87
+ def count_by_re2(re)
88
+ count = 0
89
+ scan(re) { |_| count += 1 }
90
+ count
91
+ end
92
+
93
+ end
94
+ end
95
+ end