Reputation: 44370
I trying to define a helper method that takes the Enumerable
as arguments and should return processed values. Look at example of my problem.
>> def bar
>> yield(->(x) { x.nil? })
>> end
>> bar { [nil, nil, 1].reject }
=> #<Enumerator: [nil, nil, 1]:reject>
I can't understand. An example below works as expected:
>> [nil, nil, 1].reject { |x| x.nil? }
=> [1]
What is missing here? Why i get a Enumerator
instance instead of result?
Upvotes: 0
Views: 52
Reputation: 121000
The codeblock is not passed automagically to everything.
def bar
yield(->(x) { x.nil? })
end
bar do |p|
[nil, 1].reject &p
end
When you write
bar { ... }
you actually omit the yielded variable, exactly as in:
[1,nil].each { puts 'hey' }
Upvotes: 2