underglow 0.0.5 → 0.0.6
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.
- data/lib/underglow/extensions/string.rb +8 -2
- data/lib/underglow/version.rb +1 -1
- data/spec/extensions/string_spec.rb +25 -0
- metadata +1 -1
@@ -103,14 +103,20 @@ class String
|
|
103
103
|
end
|
104
104
|
|
105
105
|
# Removes matched portion from string and returns matched data object
|
106
|
-
def extract!(regexp)
|
106
|
+
def extract!(regexp, &block)
|
107
107
|
raise ArgumentError, "Must pass in a Regexp object!" unless regexp.is_a? Regexp
|
108
108
|
|
109
|
+
|
109
110
|
match = regexp.match(self)
|
110
111
|
|
111
112
|
if match
|
112
113
|
sub!(regexp, "")
|
113
|
-
|
114
|
+
|
115
|
+
if block_given?
|
116
|
+
block.call(match)
|
117
|
+
else
|
118
|
+
return match
|
119
|
+
end
|
114
120
|
end
|
115
121
|
|
116
122
|
nil
|
data/lib/underglow/version.rb
CHANGED
@@ -81,6 +81,8 @@ describe "String" do
|
|
81
81
|
end
|
82
82
|
|
83
83
|
describe '#extract!' do
|
84
|
+
let(:str) { "Hello World!" }
|
85
|
+
|
84
86
|
it "removes the match and returns match object" do
|
85
87
|
str = "What the fuck"
|
86
88
|
match = str.extract!(/the (fuck)/)
|
@@ -107,5 +109,28 @@ describe "String" do
|
|
107
109
|
it "raises an argument error if not passed in a regexp" do
|
108
110
|
expect { "oh boy".extract!("1234") }.to raise_error ArgumentError
|
109
111
|
end
|
112
|
+
|
113
|
+
it "can accept a block with the match object passed to it if matched" do
|
114
|
+
moo = nil
|
115
|
+
|
116
|
+
str.extract!(/Hello/) do |match|
|
117
|
+
match.should be_a MatchData
|
118
|
+
moo = "what"
|
119
|
+
end
|
120
|
+
|
121
|
+
str.should == " World!"
|
122
|
+
moo.should == "what"
|
123
|
+
end
|
124
|
+
|
125
|
+
it "will not execute the block if nothing matched" do
|
126
|
+
moo = nil
|
127
|
+
|
128
|
+
str.extract!(/asdf/) do |match|
|
129
|
+
moo = "what"
|
130
|
+
end
|
131
|
+
|
132
|
+
str.should == "Hello World!"
|
133
|
+
moo.should be_nil
|
134
|
+
end
|
110
135
|
end
|
111
136
|
end
|