Project

General

Profile

Actions

Feature #12057

open

Allow methods with `yield` to be called without a block

Added by alexeymuranov (Alexey Muranov) about 8 years ago. Updated over 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:73735]

Description

Trying to figure out how yield works in Python, i had the following idea.

Allow a method with yield to be called without a block. When a method encounters yield, if no block is given, the method returns an Enumerator object.

def f
  yield 'a'
  yield 'b'
end

e = f
puts e.next  # => a
puts e.next  # => b

It seems that this is what yield in Python does, but in Python a function with yield cannot take a block. Why not to have both?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0