Module: Enumerable
- Defined in:
- lib/fat_core/enumerable.rb
Overview
Useful extensions to the core Enumerable module.
Instance Method Summary collapse
-
#each_with_flags ⇒ Object
Yield each item together with two booleans that indicate whether the item is the first or last item in the Enumerable.
Instance Method Details
#each_with_flags ⇒ Object
Yield each item together with two booleans that indicate whether the item is the first or last item in the Enumerable.
('a'..'z').to_a.each with_flags do |letter, first, last|
if first
# do something special for 'a'
elsif last
# do something special for 'z'
else
# a middling item
end
end
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fat_core/enumerable.rb', line 17 def each_with_flags # Test for beginless range return if is_a?(Range) && self.begin.nil? last_k = size - 1 each_with_index do |v, k| first = k.zero? last = (k == last_k) yield(v, first, last) end end |