0

I have created my own Enumerable method my_none?. It gives me lint error:

Convert if nested inside else to elsif.

Also two of the test cases are not working. Can anyone help me to resolve these issues?

module Enumerable
  def my_each
    i = 0
    while i < size
      yield self[i]
      i += 1
    end
  end

  def my_none?
    my_each do |item|
      if block_given?
        return false if yield item
       else
         return false if item
      end
    end
    true
  end
end
%w{ant bear cat}.my_none? { |word| word.length == 5 } #=> true
%w{ant bear cat}.my_none? { |word| word.length >= 4 } #=> false
[].my_none?                                           #=> true
[nil].my_none?                                        #=> true
[nil, false].my_none?                                 #=> true
[nil, false, true].my_none?                           #=> false

The below two test cases are not working:

%w{ant bear cat}.my_none?(/d/)                        #=> true > gives me wrong no of argument given 1 expected 0
[1, 3.14, 42].my_none?(Float)                         #=> false > gives me wrong number of argument 
Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64

1 Answers1

0

my_none? method, as you defined it, does not accept any arguments. It can only accept block.

I think you meant:

%w{ant bear cat}.my_none?{|e| e =~ /d/ }
[1, 3.14, 42].my_none?(&:to_f)

&:to_f is a way to create a Proc - see this question for more info.

To get rid of the lint errors you can change my_none? implementation to:

def my_none?
  my_each do |item|
    return false if block_given? && yield(item) || !block_given? && item
  end
  true
end
ArtuX
  • 364
  • 2
  • 11