[#91458] [Ruby trunk Feature#4475] default variable name for parameter — matz@...
Issue #4475 has been updated by matz (Yukihiro Matsumoto).
3 messages
2019/02/07
[ruby-core:91614] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc
From:
shevegen@...
Date:
2019-02-24 17:16:53 UTC
List:
ruby-core #91614
Issue #15618 has been updated by shevegen (Robert A. Heiler).
I have no particularly strong pro or con opinion on the functionality itself.
I have only one comment about syntax, though. While { |line| y << line } }
may or may not be redundant (let's leave that open for the moment), and is
definitely longer than the other variant suggested, I think it is actually a
simpler-to-understand syntax compared to the .each_line(&y) variant.
----------------------------------------
Feature #15618: Implement Enumerator::Yielder#to_proc
https://bugs.ruby-lang.org/issues/15618#change-76880
* Author: knu (Akinori MUSHA)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
When writing an Enumerator block, you often want to delegate iteration to another method like this:
```ruby
enum = Enumerator.new { |y|
Dir.glob("*.rb") { |file|
File.open(file) { |f| f.each_line { |line| y << line } }
}
}
```
I think this is such a common pattern, but the `{ |var| y << var }` part looks redundant compared to a normal iterator method being able to delegate to another method as simply as follows:
```
def each(&block)
@children.each(&block)
end
```
So, I propose adding #to_proc to Yielder so you can directly pass a yielder object to another method as a block argument.
```ruby
enum = Enumerator.new { |y|
Dir.glob("*.rb") { |file|
File.open(file) { |f| f.each_line(&y) }
}
}
```
Yielder is all about yielding, so I think it's pretty obvious what it means.
---Files--------------------------------
0001-Implement-Enumerator-Yielder-to_proc.patch (3.21 KB)
--
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>