I'm doing Ruby exercises for the Odin Project (programming newcomer), and we're tasked with recreating Ruby's #count method. Given an array like:

```
nil_list = [false, false, nil]

```

Observations:

* nil\_list.count == 3, its length.
* nil\_list.count(nil) == 1, the number of times nil is present in the list.
* nil\_list given a block behaves as expected.

When I try to recreate it, here's what I come up with:

```
module Enumerable
  def my_count (find = nil)
    result = 0
    for i in self
      if block_given?
        result += 1 if yield(i)
      elsif find != nil
        result += 1 if i == find
      else return self.length
      end
    end
    return result
  end
end

```

The problem here is that this doesn't actually count nils if we enter nil in as an argument, since this is the same (according to my code) as there not being an argument.

ie, nil\_list.my\_count(nil) == 3 instead of 1.

While typing this question I had a slightly different idea:

```
module Enumerable
  def my_count (find = "")
    result = 0
    for i in self
      if block_given?
        result += 1 if yield(i)
      elsif find != ""
        result += 1 if i == find
      else return self.length
      end
    end
    return result
  end
end

```

So this fixes the problem I was having with searches for nil, but now nil\_list.count("") == 0 whereas nil\_list.my\_count("") == 3. Same issue, just relocated to "" which I assume doesn't ever get used.

At this point I'm just curious: how does the actual count method prevent this issue from happening?