Feature #17663
openEnumerator#with, an alternative to Enumerator#with_object
Description
Enumerator#with yields each element along with the arguments
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.
Normally to apply the argument we enclosed it in a block, like so:
I found Enumerator#with_object method awkward to use.
Tried simplifying this code further, but Enumerator#with_object ignores the given block and just returns the argument.
Compare to how concise this line using Enumerator#with
Updated by RichOrElse (Ritchie Buitre) over 5 years ago
- Description updated (diff)
Updated by RichOrElse (Ritchie Buitre) over 5 years ago
- Subject changed from Enumerator#with, an alternative for Enumerator#with_object to Enumerator#with, an alternative to Enumerator#with_object
Updated by RichOrElse (Ritchie Buitre) over 5 years ago
- Description updated (diff)
Updated by Hanmac (Hans Mackowiak) over 5 years ago
Updated by RichOrElse (Ritchie Buitre) over 5 years ago
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, which is not intended for specifying the second parameter without the first.