This is Week 5 of DBC, and this week we did a Ruby group project with our accountability groups where each person had a role and corresponding problems to solve for their role. Role 4 was to write an array_deletion_method and a hash_deletion_method, both of those being bang methods. Here's how Christyn Budzyna and I solved it:
Then today, I happened to read the
"Guide
to Ruby Collections III: Enumerable and Enumerator" article that Emmanuel posted, which mentioned
a very intesting method that I have only hazily seen before, and that primarily in the context of
CLI: the grep
method.
According to Enumerable#grep,
grep
iterates over the collection object it is called on and returns an array
of values which belong to the same case (===)
as the given pattern:["a", 1, "b"].grep(String)
=> ["a", "b"]
.
So to illustrate, here's our my_array_deletion_method!
with grep
:
8 lines down to 2, not bad!