Skip to content

Commit 4b7489b

Browse files
committed
Add grep/grep_v to instance method
1 parent 6c02314 commit 4b7489b

File tree

2 files changed

+34
-70
lines changed

2 files changed

+34
-70
lines changed

mrblib/rf/container.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def hash?
3838
_.instance_of?(Hash)
3939
end
4040

41+
def array?
42+
_.instance_of?(Array)
43+
end
44+
4145
def puts(*)
4246
$output.write(generate_line_prefix)
4347
$output.puts(*)
@@ -62,6 +66,18 @@ def generate_line_prefix
6266
end
6367
end
6468

69+
%i[dig].each do |sym|
70+
define_method(sym) do |*args|
71+
_.__send__(sym, *args) if hash?
72+
end
73+
end
74+
75+
%i[grep grep_v].each do |sym|
76+
define_method(sym) do |*args, &block|
77+
_.__send__(sym, *args) if array?
78+
end
79+
end
80+
6581
def match(condition)
6682
regexp = if condition.is_a?(Regexp)
6783
condition
@@ -83,12 +99,6 @@ def match?(condition)
8399
end
84100
alias m? match?
85101

86-
%i[dig].each do |sym|
87-
define_method(sym) do |*args|
88-
_.__send__(sym, *args) if hash?
89-
end
90-
end
91-
92102
def respond_to_missing?(sym, *)
93103
# check for _0, _1, _2, _3, ...
94104
sym.to_s =~ /\A_(0|[1-9]\d*)\z/ || super

spec/container/instance_methods_spec.rb

+18-64
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@
2727
it_behaves_like 'a successful exec'
2828
end
2929

30+
where(:method, :expect_output) do
31+
'grep' | %w[bar].join("\n")
32+
'grep_v' | %w[foo baz].join("\n")
33+
end
34+
35+
with_them do
36+
let(:input) do
37+
<<~INPUT
38+
foo
39+
bar
40+
baz
41+
INPUT
42+
end
43+
let(:args) { %(-s '#{method}(/bar/)') }
44+
45+
it_behaves_like 'a successful exec'
46+
end
47+
3048
%w[match m].each do |method|
3149
describe "Container##{method}" do
3250
where do
@@ -214,67 +232,3 @@
214232
end
215233
end
216234
end
217-
218-
# describe 'Method' do
219-
#
220-
#
221-
# describe '#sub' do
222-
# let(:input) { 'foofoo' }
223-
# let(:output) do
224-
# <<~OUTPUT
225-
# barfoo
226-
# foofoo
227-
# OUTPUT
228-
# end
229-
#
230-
# before { run_rf(%q('puts sub(/foo/, "bar"); _'), input) }
231-
#
232-
# it { expect(last_command_started).to be_successfully_executed }
233-
# it { expect(last_command_started).to have_output output_string_eq output }
234-
# end
235-
#
236-
# describe '#sub!' do
237-
# let(:input) { 'foofoo' }
238-
# let(:output) do
239-
# <<~OUTPUT
240-
# barfoo
241-
# barfoo
242-
# OUTPUT
243-
# end
244-
#
245-
# before { run_rf(%q('puts sub!(/foo/, "bar"); _'), input) }
246-
#
247-
# it { expect(last_command_started).to be_successfully_executed }
248-
# it { expect(last_command_started).to have_output output_string_eq output }
249-
# end
250-
#
251-
# describe '#tr' do
252-
# let(:input) { 'foo' }
253-
# let(:output) do
254-
# <<~OUTPUT
255-
# FOO
256-
# foo
257-
# OUTPUT
258-
# end
259-
#
260-
# before { run_rf(%q('puts tr("a-z", "A-Z"); _'), input) }
261-
#
262-
# it { expect(last_command_started).to be_successfully_executed }
263-
# it { expect(last_command_started).to have_output output_string_eq output }
264-
# end
265-
#
266-
# describe '#tr!' do
267-
# let(:input) { 'foo' }
268-
# let(:output) do
269-
# <<~OUTPUT
270-
# FOO
271-
# FOO
272-
# OUTPUT
273-
# end
274-
#
275-
# before { run_rf(%q('puts tr!("a-z", "A-Z"); _'), input) }
276-
#
277-
# it { expect(last_command_started).to be_successfully_executed }
278-
# it { expect(last_command_started).to have_output output_string_eq output }
279-
# end
280-
# end

0 commit comments

Comments
 (0)