underglow 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- return match
114
+
115
+ if block_given?
116
+ block.call(match)
117
+ else
118
+ return match
119
+ end
114
120
  end
115
121
 
116
122
  nil
@@ -1,3 +1,3 @@
1
1
  module Underglow
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: underglow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: