Class: Enumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/with_else.rb

Instance Method Summary collapse

Instance Method Details

#with_else(f, *args) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/with_else.rb', line 2

def with_else(f, *args)
  if block_given?
    each {|*i| yield(*i)}
    f.call(*args)
  else
    Enumerator.new do |y|
      each do |*i|
        a = (i.size <= 1) ? i[0] : i
        y << a
      end
      f.call(*args)
    end
  end
end