Question

In recreating a the Enumerable module for practice, I have #any figured out.

  def my_any?
    everything = false
    self.each do |item| #I switched this `each`. Originally, I had written `my_each`
      everything = true if yield(item)
    end
    everything
  end

Now, to create a #none? all I have to do is this, right?

  def my_none?
    !(my_any?)
  end

However, when I call the method, I get an error:

arr = [1,2,3]
arr.my_none?{|x| x>2}
LocalJumpError: no block given (yield)
Was it helpful?

Solution

You are using the yield keyword in my_any?, which requires a block. You can capture the block given to my_none? and pass it along:

def my_none? &blk
  !(my_any? &blk)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top