Class: Enumerable::Filter
- Inherits:
-
Enumerator
- Object
- Enumerator
- Enumerable::Filter
- Defined in:
- lib/enumerable_lz/filter.rb,
lib/enumerable_lz/filter_18.rb,
lib/enumerable_lz/filter_mrb.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#filter(pattern = nil, &block) ⇒ Object
[override].
- #filter!(pattern = nil, &block) ⇒ Object
-
#initialize(obj, *args) ⇒ Filter
constructor
A new instance of Filter.
Constructor Details
#initialize(obj, *args) ⇒ Filter
Returns a new instance of Filter.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/enumerable_lz/filter.rb', line 12 def initialize obj, *args the_filter = args.shift init_block, the_filter = [the_filter, args.shift] unless args.empty? @org_enum = obj @init_block = init_block unless init_block.nil? super() do |y| @init_block.call unless @init_block.nil? compiled_filter = (@filter||[Proc.new{true}]).inject do |r,f| Proc.new{|el| r===el && f===el} end catch :do_break do @org_enum.each do |el| y.yield el if compiled_filter===el end end end filter! the_filter if the_filter end |
Instance Method Details
#each(&block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/enumerable_lz/filter_18.rb', line 20 def each &block return self unless block_given? @init_block.call unless @init_block.nil? compiled_filter = (@filter||[Proc.new{true}]).inject do |r,f| Proc.new{|el| r[el] && f[el]} end catch :do_break do @org_enum.each do |el| block.call(el) if compiled_filter[el] end end self end |
#filter(pattern = nil, &block) ⇒ Object
- override
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/enumerable_lz/filter.rb', line 42 def filter pattern=nil, &block return super unless @init_block.nil? # clone.filter! pattern, &block patterns = @filter.nil? ? [] : @filter.clone if pattern.is_a? Array patterns.push(*pattern) else patterns<<(pattern || block) end Filter.new @org_enum, patterns end |
#filter!(pattern = nil, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/enumerable_lz/filter.rb', line 31 def filter! pattern=nil, &block @filter||=[] if pattern.is_a? Array pattern.each{|el| @filter << conv_proc(el)} else @filter << conv_proc(pattern || block) end self end |