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.
- checksums.yaml +4 -4
- metadata +20 -324
- data/COPYING +0 -663
- data/ChangeLog +0 -7
- data/README +0 -62
- data/Rakefile +0 -47
- data/lib/nuggets.rb +0 -73
- data/lib/nuggets/all.rb +0 -36
- data/lib/nuggets/all_mixins.rb +0 -34
- data/lib/nuggets/ansicolor2css.rb +0 -125
- data/lib/nuggets/argv/option.rb +0 -3
- data/lib/nuggets/argv/option_mixin.rb +0 -85
- data/lib/nuggets/array/boost.rb +0 -5
- data/lib/nuggets/array/boost_mixin.rb +0 -78
- data/lib/nuggets/array/combination.rb +0 -63
- data/lib/nuggets/array/correlation.rb +0 -5
- data/lib/nuggets/array/correlation_mixin.rb +0 -64
- data/lib/nuggets/array/flatten_once.rb +0 -57
- data/lib/nuggets/array/flush.rb +0 -5
- data/lib/nuggets/array/flush_mixin.rb +0 -47
- data/lib/nuggets/array/format.rb +0 -66
- data/lib/nuggets/array/hashify.rb +0 -5
- data/lib/nuggets/array/hashify_mixin.rb +0 -48
- data/lib/nuggets/array/histogram.rb +0 -5
- data/lib/nuggets/array/histogram_mixin.rb +0 -170
- data/lib/nuggets/array/in_order.rb +0 -51
- data/lib/nuggets/array/limit.rb +0 -5
- data/lib/nuggets/array/limit_mixin.rb +0 -57
- data/lib/nuggets/array/mean.rb +0 -5
- data/lib/nuggets/array/mean_mixin.rb +0 -203
- data/lib/nuggets/array/median.rb +0 -5
- data/lib/nuggets/array/median_mixin.rb +0 -74
- data/lib/nuggets/array/mode.rb +0 -5
- data/lib/nuggets/array/mode_mixin.rb +0 -70
- data/lib/nuggets/array/monotone.rb +0 -84
- data/lib/nuggets/array/only.rb +0 -42
- data/lib/nuggets/array/rand.rb +0 -45
- data/lib/nuggets/array/regression.rb +0 -5
- data/lib/nuggets/array/regression_mixin.rb +0 -150
- data/lib/nuggets/array/runiq.rb +0 -5
- data/lib/nuggets/array/runiq_mixin.rb +0 -53
- data/lib/nuggets/array/shuffle.rb +0 -133
- data/lib/nuggets/array/standard_deviation.rb +0 -5
- data/lib/nuggets/array/standard_deviation_mixin.rb +0 -51
- data/lib/nuggets/array/to_hash.rb +0 -65
- data/lib/nuggets/array/variance.rb +0 -5
- data/lib/nuggets/array/variance_mixin.rb +0 -82
- data/lib/nuggets/cli.rb +0 -8
- data/lib/nuggets/content_type.rb +0 -98
- data/lib/nuggets/dotted_decimal.rb +0 -60
- data/lib/nuggets/enumerable/agrep.rb +0 -80
- data/lib/nuggets/enumerable/all_any_extended.rb +0 -67
- data/lib/nuggets/enumerable/minmax.rb +0 -103
- data/lib/nuggets/env/set.rb +0 -3
- data/lib/nuggets/env/set_mixin.rb +0 -68
- data/lib/nuggets/env/user_encoding.rb +0 -3
- data/lib/nuggets/env/user_encoding_mixin.rb +0 -55
- data/lib/nuggets/env/user_home.rb +0 -3
- data/lib/nuggets/env/user_home_mixin.rb +0 -57
- data/lib/nuggets/file/ext.rb +0 -5
- data/lib/nuggets/file/ext_mixin.rb +0 -88
- data/lib/nuggets/file/replace.rb +0 -5
- data/lib/nuggets/file/replace_mixin.rb +0 -56
- data/lib/nuggets/file/sub.rb +0 -5
- data/lib/nuggets/file/sub_mixin.rb +0 -96
- data/lib/nuggets/file/which.rb +0 -5
- data/lib/nuggets/file/which_mixin.rb +0 -73
- data/lib/nuggets/hash/at.rb +0 -69
- data/lib/nuggets/hash/deep_fetch.rb +0 -5
- data/lib/nuggets/hash/deep_fetch_mixin.rb +0 -75
- data/lib/nuggets/hash/deep_merge.rb +0 -5
- data/lib/nuggets/hash/deep_merge_mixin.rb +0 -54
- data/lib/nuggets/hash/idmap.rb +0 -5
- data/lib/nuggets/hash/idmap_mixin.rb +0 -42
- data/lib/nuggets/hash/in_order.rb +0 -44
- data/lib/nuggets/hash/insert.rb +0 -54
- data/lib/nuggets/hash/nest.rb +0 -5
- data/lib/nuggets/hash/nest_mixin.rb +0 -79
- data/lib/nuggets/hash/only.rb +0 -53
- data/lib/nuggets/hash/seen.rb +0 -5
- data/lib/nuggets/hash/seen_mixin.rb +0 -59
- data/lib/nuggets/hash/unroll.rb +0 -5
- data/lib/nuggets/hash/unroll_mixin.rb +0 -89
- data/lib/nuggets/hash/zip.rb +0 -5
- data/lib/nuggets/hash/zip_mixin.rb +0 -160
- data/lib/nuggets/i18n.rb +0 -156
- data/lib/nuggets/integer/factorial.rb +0 -57
- data/lib/nuggets/integer/length.rb +0 -5
- data/lib/nuggets/integer/length_mixin.rb +0 -50
- data/lib/nuggets/integer/map.rb +0 -5
- data/lib/nuggets/integer/map_mixin.rb +0 -43
- data/lib/nuggets/integer/to_binary_s.rb +0 -39
- data/lib/nuggets/io/agrep.rb +0 -44
- data/lib/nuggets/io/interact.rb +0 -5
- data/lib/nuggets/io/interact_mixin.rb +0 -160
- data/lib/nuggets/io/modes.rb +0 -122
- data/lib/nuggets/io/null.rb +0 -5
- data/lib/nuggets/io/null_mixin.rb +0 -41
- data/lib/nuggets/io/redirect.rb +0 -5
- data/lib/nuggets/io/redirect_mixin.rb +0 -51
- data/lib/nuggets/lazy_attr.rb +0 -45
- data/lib/nuggets/log_parser.rb +0 -71
- data/lib/nuggets/log_parser/apache.rb +0 -102
- data/lib/nuggets/log_parser/rails.rb +0 -220
- data/lib/nuggets/lsi.rb +0 -8
- data/lib/nuggets/midos.rb +0 -8
- data/lib/nuggets/mysql.rb +0 -8
- data/lib/nuggets/net/success.rb +0 -60
- data/lib/nuggets/numeric/between.rb +0 -2
- data/lib/nuggets/numeric/duration.rb +0 -101
- data/lib/nuggets/numeric/limit.rb +0 -63
- data/lib/nuggets/numeric/signum.rb +0 -53
- data/lib/nuggets/numeric/to_multiple.rb +0 -62
- data/lib/nuggets/object/blank.rb +0 -20
- data/lib/nuggets/object/blank_mixin.rb +0 -100
- data/lib/nuggets/object/boolean.rb +0 -5
- data/lib/nuggets/object/boolean_mixin.rb +0 -62
- data/lib/nuggets/object/eigenclass.rb +0 -2
- data/lib/nuggets/object/ghost_class.rb +0 -2
- data/lib/nuggets/object/metaclass.rb +0 -2
- data/lib/nuggets/object/msend.rb +0 -5
- data/lib/nuggets/object/msend_mixin.rb +0 -44
- data/lib/nuggets/object/silence.rb +0 -5
- data/lib/nuggets/object/silence_mixin.rb +0 -45
- data/lib/nuggets/object/singleton_class.rb +0 -5
- data/lib/nuggets/object/singleton_class_mixin.rb +0 -96
- data/lib/nuggets/object/uniclass.rb +0 -2
- data/lib/nuggets/object/virtual_class.rb +0 -2
- data/lib/nuggets/pluggable.rb +0 -92
- data/lib/nuggets/proc/bind.rb +0 -5
- data/lib/nuggets/proc/bind_mixin.rb +0 -52
- data/lib/nuggets/range/quantile.rb +0 -5
- data/lib/nuggets/range/quantile_mixin.rb +0 -43
- data/lib/nuggets/rdf/compression.rb +0 -6
- data/lib/nuggets/rdf/prefix.rb +0 -6
- data/lib/nuggets/rdf/turtle.rb +0 -8
- data/lib/nuggets/rdf/turtle/reader.rb +0 -6
- data/lib/nuggets/rdf/uri.rb +0 -6
- data/lib/nuggets/ruby.rb +0 -236
- data/lib/nuggets/statistics.rb +0 -12
- data/lib/nuggets/statistics_mixins.rb +0 -12
- data/lib/nuggets/string/camelscore.rb +0 -5
- data/lib/nuggets/string/camelscore_mixin.rb +0 -117
- data/lib/nuggets/string/capitalize_first.rb +0 -47
- data/lib/nuggets/string/case.rb +0 -82
- data/lib/nuggets/string/evaluate.rb +0 -5
- data/lib/nuggets/string/evaluate_mixin.rb +0 -48
- data/lib/nuggets/string/msub.rb +0 -85
- data/lib/nuggets/string/nsub.rb +0 -66
- data/lib/nuggets/string/sub_with_md.rb +0 -112
- data/lib/nuggets/string/wc.rb +0 -5
- data/lib/nuggets/string/wc_mixin.rb +0 -96
- data/lib/nuggets/string/word_wrap.rb +0 -77
- data/lib/nuggets/string/xor.rb +0 -5
- data/lib/nuggets/string/xor_mixin.rb +0 -60
- data/lib/nuggets/tempfile/open.rb +0 -58
- data/lib/nuggets/uri/content_type.rb +0 -5
- data/lib/nuggets/uri/content_type_mixin.rb +0 -48
- data/lib/nuggets/uri/exist.rb +0 -5
- data/lib/nuggets/uri/exist_mixin.rb +0 -57
- data/lib/nuggets/uri/redirect.rb +0 -5
- data/lib/nuggets/uri/redirect_mixin.rb +0 -102
- data/lib/nuggets/util/ansicolor2css.rb +0 -4
- data/lib/nuggets/util/cli.rb +0 -4
- data/lib/nuggets/util/content_type.rb +0 -4
- data/lib/nuggets/util/dotted_decimal.rb +0 -3
- data/lib/nuggets/util/i18n.rb +0 -4
- data/lib/nuggets/util/lazy_attr.rb +0 -4
- data/lib/nuggets/util/log_parser.rb +0 -4
- data/lib/nuggets/util/log_parser/apache.rb +0 -4
- data/lib/nuggets/util/log_parser/rails.rb +0 -4
- data/lib/nuggets/util/midos.rb +0 -4
- data/lib/nuggets/util/mysql.rb +0 -4
- data/lib/nuggets/util/pluggable.rb +0 -4
- data/lib/nuggets/util/ruby.rb +0 -4
- data/lib/nuggets/version.rb +0 -27
- data/spec/nuggets/array/boost_spec.rb +0 -50
- data/spec/nuggets/array/combination_spec.rb +0 -25
- data/spec/nuggets/array/correlation_spec.rb +0 -81
- data/spec/nuggets/array/flatten_once_spec.rb +0 -16
- data/spec/nuggets/array/flush_spec.rb +0 -43
- data/spec/nuggets/array/format_spec.rb +0 -52
- data/spec/nuggets/array/hashify_spec.rb +0 -41
- data/spec/nuggets/array/histogram_spec.rb +0 -87
- data/spec/nuggets/array/in_order_spec.rb +0 -13
- data/spec/nuggets/array/limit_spec.rb +0 -62
- data/spec/nuggets/array/mean_spec.rb +0 -203
- data/spec/nuggets/array/median_spec.rb +0 -77
- data/spec/nuggets/array/mode_spec.rb +0 -57
- data/spec/nuggets/array/monotone_spec.rb +0 -30
- data/spec/nuggets/array/only_spec.rb +0 -26
- data/spec/nuggets/array/regression_spec.rb +0 -54
- data/spec/nuggets/array/runiq_spec.rb +0 -25
- data/spec/nuggets/array/standard_deviation_spec.rb +0 -33
- data/spec/nuggets/array/to_hash_spec.rb +0 -28
- data/spec/nuggets/array/variance_spec.rb +0 -106
- data/spec/nuggets/dotted_decimal_spec.rb +0 -27
- data/spec/nuggets/enumerable/all_any_extended_spec.rb +0 -31
- data/spec/nuggets/enumerable/minmax_spec.rb +0 -21
- data/spec/nuggets/env/set_spec.rb +0 -29
- data/spec/nuggets/env/user_encoding_spec.rb +0 -38
- data/spec/nuggets/env/user_home_spec.rb +0 -42
- data/spec/nuggets/file/ext_spec.rb +0 -38
- data/spec/nuggets/file/replace_spec.rb +0 -95
- data/spec/nuggets/file/sub_spec.rb +0 -149
- data/spec/nuggets/file/which_spec.rb +0 -22
- data/spec/nuggets/hash/at_spec.rb +0 -19
- data/spec/nuggets/hash/deep_fetch_spec.rb +0 -159
- data/spec/nuggets/hash/deep_merge_spec.rb +0 -78
- data/spec/nuggets/hash/in_order_spec.rb +0 -12
- data/spec/nuggets/hash/insert_spec.rb +0 -13
- data/spec/nuggets/hash/nest_spec.rb +0 -102
- data/spec/nuggets/hash/only_spec.rb +0 -29
- data/spec/nuggets/hash/seen_spec.rb +0 -36
- data/spec/nuggets/hash/unroll_spec.rb +0 -68
- data/spec/nuggets/i18n_spec.rb +0 -13
- data/spec/nuggets/integer/factorial_spec.rb +0 -10
- data/spec/nuggets/integer/length_spec.rb +0 -18
- data/spec/nuggets/integer/map_spec.rb +0 -19
- data/spec/nuggets/integer/to_binary_s_spec.rb +0 -19
- data/spec/nuggets/numeric/duration_spec.rb +0 -25
- data/spec/nuggets/numeric/limit_spec.rb +0 -16
- data/spec/nuggets/numeric/signum_spec.rb +0 -16
- data/spec/nuggets/numeric/to_multiple_spec.rb +0 -16
- data/spec/nuggets/object/blank_spec.rb +0 -34
- data/spec/nuggets/object/boolean_spec.rb +0 -23
- data/spec/nuggets/object/msend_spec.rb +0 -25
- data/spec/nuggets/object/silence_spec.rb +0 -36
- data/spec/nuggets/object/singleton_class_spec.rb +0 -51
- data/spec/nuggets/proc/bind_spec.rb +0 -28
- data/spec/nuggets/range/quantile_spec.rb +0 -33
- data/spec/nuggets/string/camelscore_spec.rb +0 -114
- data/spec/nuggets/string/capitalize_first_spec.rb +0 -13
- data/spec/nuggets/string/case_spec.rb +0 -31
- data/spec/nuggets/string/evaluate_spec.rb +0 -24
- data/spec/nuggets/string/msub_spec.rb +0 -20
- data/spec/nuggets/string/nsub_spec.rb +0 -13
- data/spec/nuggets/string/sub_with_md_spec.rb +0 -25
- data/spec/nuggets/string/wc_spec.rb +0 -73
- data/spec/nuggets/string/word_wrap_spec.rb +0 -81
- data/spec/nuggets/string/xor_spec.rb +0 -57
- data/spec/nuggets/uri/content_type_spec.rb +0 -42
- data/spec/nuggets/uri/exist_spec.rb +0 -49
- data/spec/spec_helper.rb +0 -36
@@ -1,47 +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 String
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# str.capitalize_first => new_string
|
32
|
-
#
|
33
|
-
# Capitalizes the first character in +str+, but without downcasing the rest
|
34
|
-
# like String#capitalize does.
|
35
|
-
def capitalize_first
|
36
|
-
empty? ? self : self[0..0].upcase << self[1..-1]
|
37
|
-
end
|
38
|
-
|
39
|
-
# call-seq:
|
40
|
-
# str.capitalize_first! => str
|
41
|
-
#
|
42
|
-
# Destructive version of #capitalize_first.
|
43
|
-
def capitalize_first!
|
44
|
-
replace(capitalize_first)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
data/lib/nuggets/string/case.rb
DELETED
@@ -1,82 +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 String
|
29
|
-
|
30
|
-
module Case
|
31
|
-
|
32
|
-
LOWER = :lower
|
33
|
-
UPPER = :upper
|
34
|
-
MIXED = :mixed
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
# call-seq:
|
39
|
-
# str.case => aSymbol
|
40
|
-
#
|
41
|
-
# Returns a symbol indicating the case of _str_.
|
42
|
-
def case
|
43
|
-
self == downcase ? Case::LOWER :
|
44
|
-
self == upcase ? Case::UPPER :
|
45
|
-
Case::MIXED
|
46
|
-
end
|
47
|
-
|
48
|
-
# call-seq:
|
49
|
-
# str.lower_case? => +true+ or +false+
|
50
|
-
#
|
51
|
-
# Tell whether _str_ is all lower case.
|
52
|
-
def lower_case?
|
53
|
-
self.case == Case::LOWER
|
54
|
-
end
|
55
|
-
alias_method :downcase?, :lower_case?
|
56
|
-
|
57
|
-
# call-seq:
|
58
|
-
# str.upper_case? => +true+ or +false+
|
59
|
-
#
|
60
|
-
# Tell whether _str_ is all upper case.
|
61
|
-
def upper_case?
|
62
|
-
self.case == Case::UPPER
|
63
|
-
end
|
64
|
-
alias_method :upcase?, :upper_case?
|
65
|
-
|
66
|
-
# call-seq:
|
67
|
-
# str.mixed_case? => +true+ or +false+
|
68
|
-
#
|
69
|
-
# Tell whether _str_ is mixed case.
|
70
|
-
def mixed_case?
|
71
|
-
self.case == Case::MIXED
|
72
|
-
end
|
73
|
-
|
74
|
-
# call-seq:
|
75
|
-
# str.capitalized? => +true+ or +false+
|
76
|
-
#
|
77
|
-
# Tell whether _str_ is capitalized.
|
78
|
-
def capitalized?
|
79
|
-
self == capitalize
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
@@ -1,48 +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 String
|
30
|
-
module EvaluateMixin
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# str.evaluate([binding, [filename, [lineno]]]) => new_str
|
34
|
-
#
|
35
|
-
# Basically turns Kernel#eval into an instance method of String -- inspired
|
36
|
-
# by Ruby Cookbook example 1.3. This allows to pre-populate strings with
|
37
|
-
# substitution expressions <tt>"#{...}"</tt> that can get evaluated in a
|
38
|
-
# different environment (= +binding+) at a later point.
|
39
|
-
#
|
40
|
-
# Passes optional arguments +filename+ and +lineno+ on to Kernel#eval.
|
41
|
-
def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
|
42
|
-
buffer = gsub(/\\*"/) { |m| "#{"\\" * m.length}#{m}" }
|
43
|
-
eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
data/lib/nuggets/string/msub.rb
DELETED
@@ -1,85 +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/string/evaluate'
|
29
|
-
|
30
|
-
class String
|
31
|
-
|
32
|
-
# call-seq:
|
33
|
-
# str.msub(*substitutions) => new_str
|
34
|
-
#
|
35
|
-
# Performs _multiple_ substitutions on _str_ with order being taken into
|
36
|
-
# account (thus results of previous substitutions won't be subject to later
|
37
|
-
# ones) -- inspired by Ruby Cookbook example 1.18.
|
38
|
-
#
|
39
|
-
# The +substitutions+ parameter can be an array or a list of <tt>[pattern,
|
40
|
-
# substitution]</tt> pairs, or, simply, a hash. Note that, when using a hash,
|
41
|
-
# the ordering of how substitutions are processed might differ from what you
|
42
|
-
# intended -- instead use an array when order matters. +pattern+ can be a
|
43
|
-
# string or a regexp, +substitution+ can be a string (which may contain string
|
44
|
-
# expressions; cf. #evaluate), a proc (which will be call()ed), or any object
|
45
|
-
# really (which will be converted into a string).
|
46
|
-
def msub(*substitutions)
|
47
|
-
(_dup = dup).msub!(*substitutions) || _dup
|
48
|
-
end
|
49
|
-
|
50
|
-
# call-seq:
|
51
|
-
# str.msub!(*substitutions) => str or +nil+
|
52
|
-
#
|
53
|
-
# Destructive version of #msub.
|
54
|
-
def msub!(*substitutions)
|
55
|
-
options = substitutions.last.is_a?(::Hash) ? substitutions.pop : {}
|
56
|
-
binding = options.delete(:__binding__) || ::Kernel.binding
|
57
|
-
|
58
|
-
keys, subs, cache = [], [], {}
|
59
|
-
|
60
|
-
substitutions.concat(options.to_a).each { |key, value|
|
61
|
-
key = ::Regexp.new(::Regexp.escape(key)) unless key.is_a?(::Regexp)
|
62
|
-
|
63
|
-
keys << key
|
64
|
-
subs << [key, value]
|
65
|
-
}
|
66
|
-
|
67
|
-
gsub!(::Regexp.union(*keys)) { |match|
|
68
|
-
cache[match] ||= begin
|
69
|
-
value = subs.find { |key, _| key =~ match }.last
|
70
|
-
|
71
|
-
if value.respond_to?(:evaluate)
|
72
|
-
# make match available for string evaluation
|
73
|
-
eval("__match__ = #{match.inspect}", binding)
|
74
|
-
|
75
|
-
value.evaluate(binding)
|
76
|
-
elsif value.respond_to?(:call)
|
77
|
-
value.call(match)
|
78
|
-
else
|
79
|
-
value
|
80
|
-
end
|
81
|
-
end
|
82
|
-
}
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
data/lib/nuggets/string/nsub.rb
DELETED
@@ -1,66 +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 String
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# str.nsub(pattern, replacement, count) => new_str
|
32
|
-
# str.nsub(pattern, count) { |match| ... } => new_str
|
33
|
-
#
|
34
|
-
# Returns a copy of _str_ with the _first_ +count+ occurrences of pattern
|
35
|
-
# replaced with either +replacement+ or the value of the block.
|
36
|
-
def nsub(*args, &block)
|
37
|
-
(_dup = dup).nsub!(*args, &block) || _dup
|
38
|
-
end
|
39
|
-
|
40
|
-
# call-seq:
|
41
|
-
# str.nsub!(pattern, replacement, count) => str or +nil+
|
42
|
-
# str.nsub!(pattern, count) { |match| ... } => str or +nil+
|
43
|
-
#
|
44
|
-
# Performs the substitutions of #nsub in place, returning _str_, or +nil+ if
|
45
|
-
# no substitutions were performed.
|
46
|
-
def nsub!(*args)
|
47
|
-
pattern, i = args.first, 0
|
48
|
-
|
49
|
-
case args.size
|
50
|
-
when 2
|
51
|
-
# Only +count+ given
|
52
|
-
count = args.last
|
53
|
-
|
54
|
-
gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match }
|
55
|
-
when 3
|
56
|
-
# Both +replacement+ and +count+ given;
|
57
|
-
# ignore block (just like String#gsub does)
|
58
|
-
replacement, count = args.values_at(1, 2)
|
59
|
-
|
60
|
-
gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
|
61
|
-
else
|
62
|
-
raise ::ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
@@ -1,112 +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 String
|
29
|
-
|
30
|
-
alias_method :sub_without_md, :sub
|
31
|
-
alias_method :sub_without_md!, :sub!
|
32
|
-
alias_method :gsub_without_md, :gsub
|
33
|
-
alias_method :gsub_without_md!, :gsub!
|
34
|
-
|
35
|
-
# call-seq:
|
36
|
-
# str.sub_with_md(pattern) { |match_data| ... } => new_str
|
37
|
-
#
|
38
|
-
# Just like #sub, but passes the MatchData object instead of the current
|
39
|
-
# match string to the block.
|
40
|
-
def sub_with_md(pattern, replacement = nil, &block)
|
41
|
-
replacement ?
|
42
|
-
sub_without_md(pattern, replacement) :
|
43
|
-
(_dup = dup).sub_with_md!(pattern, &block) || _dup
|
44
|
-
end
|
45
|
-
|
46
|
-
# call-seq:
|
47
|
-
# str.sub_with_md!(pattern) { |match_data| ... } => str or +nil+
|
48
|
-
#
|
49
|
-
# Destructive version of #sub_with_md.
|
50
|
-
def sub_with_md!(pattern, replacement = nil)
|
51
|
-
replacement ?
|
52
|
-
sub_without_md!(pattern, replacement) :
|
53
|
-
sub_without_md!(pattern) { |match| yield $~ }
|
54
|
-
end
|
55
|
-
|
56
|
-
# call-seq:
|
57
|
-
# str.gsub_with_md(pattern) { |match_data| ... } => new_str
|
58
|
-
#
|
59
|
-
# Just like #gsub, but passes the MatchData object instead of the current
|
60
|
-
# match string to the block.
|
61
|
-
def gsub_with_md(pattern, replacement = nil, &block)
|
62
|
-
replacement ?
|
63
|
-
gsub_without_md(pattern, replacement) :
|
64
|
-
(_dup = dup).gsub_with_md!(pattern, &block) || _dup
|
65
|
-
end
|
66
|
-
|
67
|
-
# call-seq:
|
68
|
-
# str.gsub_with_md!(pattern) { |match_data| ... } => str or +nil+
|
69
|
-
#
|
70
|
-
# Destructive version of #gsub_with_md.
|
71
|
-
def gsub_with_md!(pattern, replacement = nil)
|
72
|
-
replacement ?
|
73
|
-
gsub_without_md!(pattern, replacement) :
|
74
|
-
gsub_without_md!(pattern) { |match| yield $~ }
|
75
|
-
end
|
76
|
-
|
77
|
-
# call-seq:
|
78
|
-
# gimme_match_data!
|
79
|
-
#
|
80
|
-
# Replaces the traditional substitution methods with their MatchData passing
|
81
|
-
# equivalents. USE WITH CAUTION!
|
82
|
-
def self.gimme_match_data!
|
83
|
-
alias_method :sub, :sub_with_md
|
84
|
-
alias_method :sub!, :sub_with_md!
|
85
|
-
alias_method :gsub, :gsub_with_md
|
86
|
-
alias_method :gsub!, :gsub_with_md!
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
if $0 == __FILE__
|
92
|
-
s = 'Foo, Bar - Baz'
|
93
|
-
p s
|
94
|
-
|
95
|
-
String.gimme_match_data!
|
96
|
-
|
97
|
-
p s.gsub(/\w(\w+)(\W*)/) { |m|
|
98
|
-
begin
|
99
|
-
"#{$1.gsub(/[ao]/, 'X')}#{$2}"
|
100
|
-
rescue NoMethodError => err
|
101
|
-
warn err
|
102
|
-
end
|
103
|
-
}
|
104
|
-
|
105
|
-
p s.gsub(/\w(\w+)(\W*)/) { |md|
|
106
|
-
"#{md[1].gsub(/[ao]/, 'X')}#{md[2]}"
|
107
|
-
}
|
108
|
-
|
109
|
-
p s.gsub(/\w(\w+)(\W*)/) { |md|
|
110
|
-
"#{md[1].gsub(/[ao]/) { |md2| md2[0].upcase }}#{md[2]}"
|
111
|
-
}
|
112
|
-
end
|