As you wrote your each_safe method, the simplest would be
curse_array.each_safe { |element| do_something_with(element) }
Edit: Oh, your every_safe method is incorrect. It should be "each of them" and not "each.do"
Edit 2: If you really want to do something like " each_safe.map ", and at the same time can also do " each_safe { ... } ", you can write your method for example:
require 'enumerator' class CurseArray < Array BLACKLIST = /bad/ def each_safe arr = [] each do |element| unless element =~ BLACKLIST if block_given? yield element else arr << element end end end unless block_given? return Enumerator.new(arr) end end end
source share