Tag: enumerable

  • Getting a Count of Occurrences of Items in a Ruby Array (and a Caveat for Rails)

    I feel like I’m often wanting to count occurrences of items in an array (Rails has its own special case as well), and I’m always trying to do it the “long way.” I finally stumbled upon this answer on StackOverflow that details the version-by-version options: Ruby 2.7+ use .tally directly on the array: Ruby 2.4+ […]

  • Ruby: Enumerable grep, grep_v

    UPDATE: After I wrote this, I started finding myself doing a lot of caller.grep(/(project_directory|suspected_gem)/) to aid in debugging obscure interactions with internal gems and projects. In looking at a pull request and noticing some awkward “first” and “last” iteration detection which also required each_with_index, I started looking into what would be a cleaner way, and […]