[#102393] [Ruby master Feature#17608] Compact and sum in one step — sawadatsuyoshi@...

Issue #17608 has been reported by sawa (Tsuyoshi Sawada).

13 messages 2021/02/04

[#102438] [Ruby master Bug#17619] if false foo=42; end creates a foo local variable set to nil — pkmuldoon@...

Issue #17619 has been reported by pkmuldoon (Phil Muldoon).

10 messages 2021/02/10

[#102631] [Ruby master Feature#17660] Expose information about which basic methods have been redefined — tenderlove@...

Issue #17660 has been reported by tenderlovemaking (Aaron Patterson).

9 messages 2021/02/27

[#102639] [Ruby master Misc#17662] The herdoc pattern used in tests does not syntax highlight correctly in many editors — eregontp@...

Issue #17662 has been reported by Eregon (Benoit Daloze).

13 messages 2021/02/27

[#102652] [Ruby master Bug#17664] Behavior of sockets changed in Ruby 3.0 to non-blocking — ciconia@...

Issue #17664 has been reported by ciconia (Sharon Rosner).

23 messages 2021/02/28

[ruby-core:102644] [Ruby master Feature#17663] Enumerator#with, an alternative for Enumerator#with_object

From: ritchie@...
Date: 2021-02-27 17:23:33 UTC
List: ruby-core #102644
Issue #17663 has been reported by RichOrElse (Ritchie Buitre).

----------------------------------------
Feature #17663: Enumerator#with, an alternative for Enumerator#with_object
https://bugs.ruby-lang.org/issues/17663

* Author: RichOrElse (Ritchie Buitre)
* Status: Open
* Priority: Normal
----------------------------------------
**Enumerator#with** yields each element along with the arguments
``` ruby
class Enumerator
  def with(*options)
    return to_enum(:with, *options) unless defined? yield

    each do |entry|
      yield entry, *options
    end
  end
end
```

I found **Enumerator#with_object** method awkward to use. Suppose we have a proc that accepts more than 1 argument.
``` ruby
format = proc do |value, *option|
  value.to_s(*option)
end
``` 
Normally to apply the argument we enclosed it in a block, like so:
``` ruby
(10..15).map { |n| format.(n, 16) } # => ["a", "b", "c", "d", "e", "f"]
```
Here's the equivalent code using **Enumerator#with_object** method.
``` ruby
(10..15).each.with_object(16).map(&format) # => ["a", "b", "c", "d", "e", "f"]
```
Tried simplifying this code further, but ** Enumerator#with_object** ignores the given block and just returns the argument.
``` ruby
(10..15).map.with_object(16, &format) # => 16
```
Compare to how concise this line using the **Enumerator#with** 
``` ruby
(10..15).map.with(16, &format)  # => ["a", "b", "c", "d", "e", "f"]

```






-- 
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>

In This Thread

Prev Next