Class: Enumerable::Enumerator::Filter
- Inherits:
-
Enumerable::Enumerator
- Object
- Enumerable::Enumerator
- Enumerable::Enumerator::Filter
- Defined in:
- lib/enumerable_lz/filter_18.rb
Direct Known Subclasses
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#filter(pattern = nil, &block) ⇒ Object
[override].
- #filter!(pattern = nil, &block) ⇒ Object
-
#initialize(obj, the_filter = nil) ⇒ Filter
constructor
A new instance of Filter.
-
#with_index(offset = 0, &block) ⇒ Object
[override].
- #with_initializer(init_proc, pattern = nil, &block) ⇒ Object
Constructor Details
#initialize(obj, the_filter = nil) ⇒ Filter
Returns a new instance of Filter.
6 7 8 9 |
# File 'lib/enumerable_lz/filter_18.rb', line 6 def initialize obj, the_filter=nil filter! the_filter if the_filter @org_enum = obj end |
Instance Method Details
#each(&block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/enumerable_lz/filter_18.rb', line 11 def each &block return self unless block_given? # compiled_filter = (@filter||[Proc.new{true}]).inject do |r,f| # Proc.new{|el| r[el] && f[el]} # end compiled_filter = @filter.nil? ? Proc.new{true} : lambda{|f| break f[0] if f.size==1 codes = f.size.times.map do |idx| "f[#{idx}][el]" end eval "Proc.new{|el|"+codes.join(" && ")+"}" }.call(@filter) 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 |
# File 'lib/enumerable_lz/filter_18.rb', line 42 def filter pattern=nil, &block # 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_18.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 |
#with_index(offset = 0, &block) ⇒ Object
- override
59 60 61 62 63 64 65 |
# File 'lib/enumerable_lz/filter_18.rb', line 59 def with_index offset=0, &block raise ArgumentError, "tried to call filter.with_index without a block" unless block_given? i = offset - 1 with_initializer(Proc.new{i = offset - 1}) do |el| block.call(el, i += 1) end end |
#with_initializer(init_proc, pattern = nil, &block) ⇒ Object
53 54 55 56 |
# File 'lib/enumerable_lz/filter_18.rb', line 53 def with_initializer init_proc, pattern = nil, &block src_enum = @filter.nil? ? @org_enum : self FilterWithInitializer.new src_enum, init_proc, pattern||block end |