From: ritchie@... Date: 2021-03-02T17:48:46+00:00 Subject: [ruby-core:102723] [Ruby master Feature#17663] Enumerator#with, an alternative to Enumerator#with_object Issue #17663 has been updated by RichOrElse (Ritchie Buitre). Hanmac (Hans Mackowiak) wrote in #note-4: > i had a similar problem when i wanted to make Symbol to_proc use parameters, i would have done something like `:to_s.(16)` Thanks, the implementation of format proc could be anything other than **Integer#to_s** . I usually define to_proc for my classes, in those scenarios I couldn't use **Symbol#call** . ``` ruby class UserPresenter < SimpleDelegator def initialize(model, view = nil) @view = view super end def self.to_proc method(:new).to_proc end end @users = User.all.map.with(view_context, &UserPresenter) ``` ---------------------------------------- Feature #17663: Enumerator#with, an alternative to Enumerator#with_object https://bugs.ruby-lang.org/issues/17663#change-90719 * 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 ``` 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"] ``` I found **Enumerator#with_object** method awkward to use. ``` 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 **Enumerator#with** ``` ruby (10..15).map.with(16, &format) # => ["a", "b", "c", "d", "e", "f"] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: