DEV Community

Burdette Lamar
Burdette Lamar

Posted on • Edited on

1

Ruby Method of the Day: #count

Method count returns an integer count of the number of items in a collection that match a given criterion.

The method is available as:

There are three ways to call the method:

  • With no argument and no block, returns the count of items in the collection:
[0, 1, 2].count # => 3
[].count # => 0
Enter fullscreen mode Exit fullscreen mode
  • With argument object, returns the count of items == object in the collection:
[0, 1, 2, 0.0].count(0) # => 2
[0, 1, 2].count(3) # => 0
Enter fullscreen mode Exit fullscreen mode
  • With a block, calls the block with each item in the collection; returns the count of elements for which the block returns a truthy value:
[0, 1, 2, 3].count {|element| element > 1} # => 2
Enter fullscreen mode Exit fullscreen mode

More methods at #rubymethodoftheday.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay