The_stupid_learning_experience
I wanted a new Array method.. maybe included in Enumerable. Maybe I could use some c-lib later and make it really fast. I wanted unique?
.
So set upon this task I was.. that I made myself blind for the existing count
-command. Sure, I knew about count.. but right then.. it sort of stopped existing.
In hindsight, I was the guy taking the bike to the hardware store for tools to invent the wheel.
- count
- with no arguments, I guess count works like
size
/length
- with one arguments, it returns an Integer: the number of occurrences in the Array.
- with no argument and a block, the block defines
true
, and count returns the count of items for which the block returns true.
But I was without count.. and iterated through the array, building a response array.
When the brain - then suddenly - is set right.. it’s almost audible. The shame-infused Eureka-moment is a fantastic experience. It’s a neighbour to hurt.. but in a good way. And you will never forget.. because you made a journey about it.
NOTE: I still want the unique?
-method. But the implementation is way more banal than before.
0 => nil, 1 => true, else => false
For count it’s a valid response to say ‘bolognese’ occurs in this array zero times.
But unique?
can’t think like that. The question-mark makes the method a Boolean question. True and False are the only valid answers. But we can’t use ‘false’ for both ‘occurs more than once’ AND ‘is not present’. So nil will return a sort of ‘illegitimate question’.