Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yield all values from another iterator

Tags:

ruby

Does Ruby provide any mechanism to allow an iterator to yield all values from another iterator? (or "subiterator", I'm not sure what the proper name is). Similar to Python3.3+'s yield from

def f
    yield 'a'
    yield 'b'
end

def g
   # yield everything from f
   yield 'c'
   yield 'd'
end
like image 742
Ryan Haining Avatar asked Jul 15 '13 17:07

Ryan Haining


1 Answers

This is probably the most idiomatic approach:

def f
  yield 'a'
  yield 'b'
end

def g(&block)
  f(&block)
  yield 'c'
  yield 'd'
end
like image 90
tokland Avatar answered Oct 09 '22 07:10

tokland