Benchmark time:
require 'fruity'
ARY = ["banana man", "apple", "banana woman"]
ARY.grep(/^banana/) # => ["banana man", "banana woman"]
ARY.select { |i| i.start_with?("bana") } # => ["banana man", "banana woman"]
compare do
grep_only { ARY.grep(/^banana/) }
select_start_with { ARY.select { |i| i.start_with?("bana") } }
end
# >> Running each test 4096 times. Test will take about 1 second.
# >> select_start_with is faster than grep_only by 3x ± 1.0
Extending ARY:
ARY = ["banana man", "apple", "banana woman"] * 1000
compare do
grep_only { ARY.grep(/^banana/) }
select_start_with { ARY.select { |i| i.start_with?("bana") } }
end
# >> Running each test 8 times. Test will take about 1 second.
# >> select_start_with is faster than grep_only by 3x ± 0.1
"banana") in advance or do you have to extract / identify it from the given strings?"apple"isolated from the other two elements? Is it because it starts with"a"?ais your array and we interpret your question literally (as we should),a.delete("apple")is sufficient (though I doubt that's what you are looking for).