Ruby: Counting occurrences of an item in an array / enumerable
Enumerable#count
Show archive.org snapshot
can do three things.
- With no argument provided, it returns the number of items.
- With an argument, it returns the number of items matching the given value.
- With a block, it counts the number of elements yielding a truthy value.
ary = [1, 2, 4, 2]
ary.count #=> 4
ary.count(2) #=> 2
ary.count { |x| x % 2 == 0 } #=> 3
Related cards:
Deal with different ways of counting weeks and weekdays in Ruby
Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this...
Javascript equivalent of Ruby's array.collect(&:method)
The most common use case for Ruby's #collect
is to call a method on each list element and collect the return values in a new array:
['hello', 'world', 'this', 'is', 'nice'].collect(&:length)
# => [5, 5, 4, 2, 4]
Although there is n...
Ruby: How to collect a Hash from an Array
There are many different methods that allow mapping an Array to a Hash in Ruby.
Array#to_h
with a block (Ruby 2.6+)
You can call an array with a block that is called with each element. The block must return a [key, value]
tuple.
This i...
Custom Ruby method Enumerable#count_by (use for quick statistics)
I frequently find myself needing a combination of group_by
, count
and sort
for quick statistics. Here's a method on Enumerable
that combines the three:
module Enumerable
def count_by(&block)
group_by(&block)
.transform_val...
How to iterate over an Enumerable, returning the first truthy result of a block ("map-find")
Ruby has Enumerable.find(&block)
, which returns the first item in the collection for which the block evaluates to true
.
first_post_with_image = posts.find do |post|
post.image
end
However, sometimes it's not the item you're int...
Ruby, Ruby on Rails, and _why: The disappearance of one of the world’s most beloved computer programmers
Nice article to educate your non-geek girlfriend/boyfriend about the joys of programming.
Ruby: How to grow or shrink an array to a given size
If you want to grow a Ruby Array, you might find out about #fill
but it is not really what you are looking for. [1]
For arrays of unknown size that you want to grow or shrink to a fixed size, you need to define something yourself. Like the follo...
Ruby: How to keep split delimiter (separate, or as part of substrings)
Ruby's String#split
returns an array of substrings from the given string. Usually, this is missing the split characters:
>> 'user@example.com'.split('@')
=> ["user", "example.com"]
If you want to join those parts later on, you migh...
How to fix "undefined method `name' for Array" error when running bundled commands on Ruby 1.8.7 + Rails 2.3
On recent/fresh installations of Ruby 1.8.7 you may encounter this error why calling any bundled binary (or just bundle exec
):
/home/arne/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in ...