[#76442] [Ruby trunk Feature#11741] Migrate Ruby to Git from Subversion — naruse@...
Issue #11741 has been updated by Yui NARUSE.
3 messages
2016/07/19
[#76515] [Ruby trunk Bug#12610] webrick: protect from httpoxy — nagachika00@...
Issue #12610 has been updated by Tomoyuki Chikanaga.
3 messages
2016/07/22
[ruby-core:76459] [CommonRuby Feature#12057] Allow methods with `yield` to be called without a block
From:
shyouhei@...
Date:
2016-07-20 02:15:29 UTC
List:
ruby-core #76459
Issue #12057 has been updated by Shyouhei Urabe.
What OP wants can be done using enumerator.
```ruby
require 'enumerator'
def f
Enumerator.new do |y|
y.yield 'a'
y.yield 'b'
end
end
e = f
puts e.next # => a
puts e.next # => b
```
Or, I might be failing to understand the request. Might it be "if block is present call it, but if absent no error" request? If so, following one line addition solves such request.
```ruby
def f
return enum_for(__method__) unless block_given? # this
yield 'a'
yield 'b'
end
e = f
puts e.next # => a
puts e.next # => b
```
Either way, I see no need to extend the language.
----------------------------------------
Feature #12057: Allow methods with `yield` to be called without a block
https://bugs.ruby-lang.org/issues/12057#change-59704
* Author: Alexey Muranov
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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.
~~~ruby
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?
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>