blackwinter-ruby-nuggets 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +676 -0
- data/ChangeLog +5 -0
- data/README +60 -0
- data/Rakefile +20 -0
- data/lib/nuggets/all.rb +34 -0
- data/lib/nuggets/array/combination.rb +86 -0
- data/lib/nuggets/array/flatten_once.rb +68 -0
- data/lib/nuggets/array/format.rb +124 -0
- data/lib/nuggets/array/in_order.rb +62 -0
- data/lib/nuggets/array/monotone.rb +101 -0
- data/lib/nuggets/array/only.rb +56 -0
- data/lib/nuggets/array/rand.rb +45 -0
- data/lib/nuggets/array/shuffle.rb +133 -0
- data/lib/nuggets/array/to_hash.rb +86 -0
- data/lib/nuggets/enumerable/agrep.rb +82 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +99 -0
- data/lib/nuggets/enumerable/minmax.rb +119 -0
- data/lib/nuggets/env/user_encoding.rb +53 -0
- data/lib/nuggets/env/user_home.rb +54 -0
- data/lib/nuggets/file/which.rb +74 -0
- data/lib/nuggets/hash/at.rb +87 -0
- data/lib/nuggets/hash/in_order.rb +52 -0
- data/lib/nuggets/hash/insert.rb +65 -0
- data/lib/nuggets/hash/only.rb +68 -0
- data/lib/nuggets/integer/factorial.rb +74 -0
- data/lib/nuggets/integer/to_binary_s.rb +47 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/modes.rb +133 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +109 -0
- data/lib/nuggets/numeric/limit.rb +70 -0
- data/lib/nuggets/numeric/signum.rb +60 -0
- data/lib/nuggets/numeric/to_multiple.rb +68 -0
- data/lib/nuggets/object/blank.rb +119 -0
- data/lib/nuggets/object/boolean.rb +69 -0
- data/lib/nuggets/object/eigenclass.rb +2 -0
- data/lib/nuggets/object/ghost_class.rb +2 -0
- data/lib/nuggets/object/metaclass.rb +2 -0
- data/lib/nuggets/object/msend.rb +55 -0
- data/lib/nuggets/object/singleton_class.rb +150 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/proc/bind.rb +68 -0
- data/lib/nuggets/string/capitalize_first.rb +63 -0
- data/lib/nuggets/string/case.rb +104 -0
- data/lib/nuggets/string/evaluate.rb +53 -0
- data/lib/nuggets/string/msub.rb +82 -0
- data/lib/nuggets/string/nsub.rb +80 -0
- data/lib/nuggets/string/sub_with_md.rb +131 -0
- data/lib/nuggets/string/word_wrap.rb +111 -0
- data/lib/nuggets/tempfile/open.rb +54 -0
- data/lib/nuggets/uri/content_type.rb +65 -0
- data/lib/nuggets/uri/exist.rb +63 -0
- data/lib/nuggets/util/added_methods/init.rb +3 -0
- data/lib/nuggets/util/added_methods.rb +407 -0
- data/lib/nuggets/util/ansicolor2css.rb +90 -0
- data/lib/nuggets/util/content_type.rb +104 -0
- data/lib/nuggets/util/dotted_decimal.rb +66 -0
- data/lib/nuggets/util/i18n.rb +143 -0
- data/lib/nuggets/version.rb +27 -0
- data/lib/nuggets.rb +73 -0
- metadata +124 -0
@@ -0,0 +1,150 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Object
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# object.singleton_class => aClass
|
32
|
+
#
|
33
|
+
# Returns the singleton (or virtual/eigen/meta) class associated with _object_.
|
34
|
+
def singleton_class
|
35
|
+
class << self; self; end
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :virtual_class, :singleton_class
|
39
|
+
alias_method :ghost_class, :singleton_class
|
40
|
+
alias_method :eigenclass, :singleton_class
|
41
|
+
alias_method :metaclass, :singleton_class
|
42
|
+
alias_method :uniclass, :singleton_class
|
43
|
+
|
44
|
+
# call-seq:
|
45
|
+
# object.singleton_object => anObject
|
46
|
+
#
|
47
|
+
# Returns the object of which _object_ is the singleton_class.
|
48
|
+
# Raises a TypeError if _object_ is not a singleton class.
|
49
|
+
def singleton_object
|
50
|
+
[true, false, nil].each { |obj|
|
51
|
+
return obj if self.equal?(obj.singleton_class)
|
52
|
+
}
|
53
|
+
|
54
|
+
# raises TypeError if neither class nor module
|
55
|
+
ObjectSpace.each_object(self) { |obj|
|
56
|
+
return obj if self.equal?(obj.singleton_class)
|
57
|
+
}
|
58
|
+
|
59
|
+
# if we got here, it can't be a singleton class
|
60
|
+
# or its singleton object doesn't exist anymore
|
61
|
+
raise TypeError
|
62
|
+
rescue TypeError
|
63
|
+
raise TypeError, 'not a singleton class'
|
64
|
+
end
|
65
|
+
|
66
|
+
alias_method :virtual_object, :singleton_object
|
67
|
+
alias_method :ghost_object, :singleton_object
|
68
|
+
alias_method :eigenobject, :singleton_object
|
69
|
+
alias_method :metaobject, :singleton_object
|
70
|
+
alias_method :uniobject, :singleton_object
|
71
|
+
|
72
|
+
# call-seq:
|
73
|
+
# object.singleton_class? => true or false
|
74
|
+
#
|
75
|
+
# Returns true if _object_ is a singleton_class
|
76
|
+
# (i.e., has a singleton_object), false otherwise.
|
77
|
+
def singleton_class?
|
78
|
+
singleton_object
|
79
|
+
true
|
80
|
+
rescue TypeError
|
81
|
+
false
|
82
|
+
end
|
83
|
+
|
84
|
+
alias_method :virtual_class?, :singleton_class?
|
85
|
+
alias_method :ghost_class?, :singleton_class?
|
86
|
+
alias_method :eigenclass?, :singleton_class?
|
87
|
+
alias_method :metaclass?, :singleton_class?
|
88
|
+
alias_method :uniclass?, :singleton_class?
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
if $0 == __FILE__
|
93
|
+
o = Object.new
|
94
|
+
p o
|
95
|
+
p o.singleton_class?
|
96
|
+
|
97
|
+
begin
|
98
|
+
p o.singleton_object
|
99
|
+
rescue TypeError => err
|
100
|
+
warn err
|
101
|
+
end
|
102
|
+
|
103
|
+
s = o.singleton_class
|
104
|
+
p s
|
105
|
+
p s.singleton_class?
|
106
|
+
p s.singleton_object
|
107
|
+
|
108
|
+
###
|
109
|
+
|
110
|
+
o = [1, 2]
|
111
|
+
p o
|
112
|
+
|
113
|
+
s = o.singleton_class
|
114
|
+
p s
|
115
|
+
p s.singleton_class?
|
116
|
+
p s.singleton_object
|
117
|
+
|
118
|
+
###
|
119
|
+
|
120
|
+
p Class.new.singleton_class?
|
121
|
+
p Class.singleton_class?
|
122
|
+
|
123
|
+
###
|
124
|
+
|
125
|
+
c = Class.new
|
126
|
+
o = c.new
|
127
|
+
p o
|
128
|
+
p c.singleton_class?
|
129
|
+
|
130
|
+
###
|
131
|
+
|
132
|
+
p nil.singleton_class
|
133
|
+
p NilClass.singleton_class?
|
134
|
+
p NilClass.singleton_object
|
135
|
+
|
136
|
+
###
|
137
|
+
|
138
|
+
class A # :nodoc:
|
139
|
+
end
|
140
|
+
class B < A # :nodoc:
|
141
|
+
end
|
142
|
+
|
143
|
+
a = A.singleton_class
|
144
|
+
b = B.singleton_class
|
145
|
+
|
146
|
+
p a
|
147
|
+
p a.singleton_object #=> A
|
148
|
+
p b
|
149
|
+
p b.singleton_object #=> B
|
150
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Proc
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# proc.bind(object) => aMethod
|
32
|
+
#
|
33
|
+
# Straight from Rails' ActiveSupport -- effectively binds _proc_ to +object+.
|
34
|
+
def bind(object)
|
35
|
+
block, time = self, Time.now
|
36
|
+
|
37
|
+
(class << object; self; end).class_eval {
|
38
|
+
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
39
|
+
define_method(method_name, &block)
|
40
|
+
|
41
|
+
method = instance_method(method_name)
|
42
|
+
remove_method(method_name)
|
43
|
+
|
44
|
+
method
|
45
|
+
}.bind(object)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
if $0 == __FILE__
|
51
|
+
l = lambda { bla }
|
52
|
+
|
53
|
+
begin
|
54
|
+
l.call
|
55
|
+
rescue NameError => err
|
56
|
+
puts "#{err} (#{err.class})"
|
57
|
+
end
|
58
|
+
|
59
|
+
module Bla # :nodoc:
|
60
|
+
|
61
|
+
def self.bla
|
62
|
+
puts 'blub'
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
l.bind(Bla).call
|
68
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# 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
|
+
return self if empty?
|
37
|
+
self[0..0].upcase << self[1..-1]
|
38
|
+
end
|
39
|
+
|
40
|
+
# call-seq:
|
41
|
+
# str.capitalize_first! => str
|
42
|
+
#
|
43
|
+
# Destructive version of #capitalize_first.
|
44
|
+
def capitalize_first!
|
45
|
+
replace capitalize_first
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
if $0 == __FILE__
|
51
|
+
s = 'Some string'
|
52
|
+
p s
|
53
|
+
p s.capitalize_first
|
54
|
+
|
55
|
+
s = 'some string'
|
56
|
+
p s
|
57
|
+
p s.capitalize_first
|
58
|
+
|
59
|
+
s = 'SOME STRING'
|
60
|
+
p s
|
61
|
+
p s.capitalize
|
62
|
+
p s.capitalize_first
|
63
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# 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
|
83
|
+
|
84
|
+
if $0 == __FILE__
|
85
|
+
s = 'Some string'
|
86
|
+
puts s
|
87
|
+
p s.case
|
88
|
+
puts s.downcase?
|
89
|
+
puts s.upcase?
|
90
|
+
puts s.mixed_case?
|
91
|
+
puts s.capitalized?
|
92
|
+
|
93
|
+
s = 'some string'
|
94
|
+
puts s
|
95
|
+
p s.case
|
96
|
+
puts s.downcase?
|
97
|
+
puts s.mixed_case?
|
98
|
+
|
99
|
+
s = 'SOME STRING'
|
100
|
+
puts s
|
101
|
+
p s.case
|
102
|
+
puts s.upcase?
|
103
|
+
puts s.mixed_case?
|
104
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class String
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# str.evaluate(binding = TOPLEVEL_BINDING) => new_str
|
32
|
+
#
|
33
|
+
# Basically turns Kernel#eval into an instance method of String -- inspired
|
34
|
+
# by Ruby Cookbook example 1.3. This allows to pre-populate strings with
|
35
|
+
# substitution expressions ("#{...}") that can get evaluated in a different
|
36
|
+
# environment (= +binding+) at a later point.
|
37
|
+
def evaluate(binding = TOPLEVEL_BINDING)
|
38
|
+
eval(%Q{"#{self}"}, binding)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
if $0 == __FILE__
|
44
|
+
s = 'bl#{a}blub'
|
45
|
+
p s
|
46
|
+
|
47
|
+
def foo(bar) # :nodoc:
|
48
|
+
a = 'ub'
|
49
|
+
bar.evaluate(binding)
|
50
|
+
end
|
51
|
+
|
52
|
+
p foo(s)
|
53
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# 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+ may contain string expressions (cf.
|
44
|
+
# #evaluate).
|
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
|
+
substitutions = *substitutions # Handle hashes and arrays alike
|
55
|
+
keys, subs, cache = [], [], {}
|
56
|
+
|
57
|
+
substitutions.each { |key, value|
|
58
|
+
key = Regexp.new(Regexp.escape(key)) unless key.is_a?(Regexp)
|
59
|
+
|
60
|
+
keys << key
|
61
|
+
subs << [key, value]
|
62
|
+
}
|
63
|
+
|
64
|
+
gsub!(Regexp.union(*keys)) { |match|
|
65
|
+
cache[match] ||= subs.find { |key, _|
|
66
|
+
key =~ match
|
67
|
+
}.last.evaluate(binding)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
if $0 == __FILE__
|
74
|
+
s = 'Foo, Bar - Baz'
|
75
|
+
p s
|
76
|
+
|
77
|
+
p s.gsub(/a/, 'o').gsub(/o/, 'a')
|
78
|
+
p s.msub('a' => 'o', 'o' => 'a')
|
79
|
+
|
80
|
+
s.msub!('a' => 'o','o' => 'a')
|
81
|
+
p s
|
82
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# 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 General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# 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)
|
37
|
+
_dup = dup
|
38
|
+
(block_given? ?
|
39
|
+
_dup.nsub!(*args) { |*a| yield(*a) } :
|
40
|
+
_dup.nsub!(*args)) || _dup
|
41
|
+
end
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# str.nsub!(pattern, replacement, count) => str or nil
|
45
|
+
# str.nsub!(pattern, count) { |match| ... } => str or nil
|
46
|
+
#
|
47
|
+
# Performs the substitutions of #nsub in place, returning _str_, or +nil+ if
|
48
|
+
# no substitutions were performed.
|
49
|
+
def nsub!(*args)
|
50
|
+
pattern, i = args.first, 0
|
51
|
+
|
52
|
+
case args.size
|
53
|
+
when 2
|
54
|
+
# Only +count+ given
|
55
|
+
count = args.last
|
56
|
+
|
57
|
+
gsub!(pattern) { |match| (i += 1) <= count ? yield(match) : match }
|
58
|
+
when 3
|
59
|
+
# Both +replacement+ and +count+ given;
|
60
|
+
# ignore block (just like String#gsub does)
|
61
|
+
replacement, count = args.values_at(1, 2)
|
62
|
+
|
63
|
+
gsub!(pattern) { |match| (i += 1) <= count ? replacement : match }
|
64
|
+
else
|
65
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 2-3)"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
if $0 == __FILE__
|
72
|
+
s = 'a b c d e f g h i'
|
73
|
+
puts s
|
74
|
+
|
75
|
+
puts s.nsub(' ', '', 6)
|
76
|
+
puts s.nsub(' ', 6) { '' }
|
77
|
+
|
78
|
+
s.nsub!(' ', '', 6)
|
79
|
+
puts s
|
80
|
+
end
|