Skip to content
instance method

Hash#any?

() -> bool
(untyped pattern) -> bool
() { (K, V) -> ::boolish } -> bool
any? → true or false
any?(entry) → true or false
any? {|key, value| ... } → true or false

Returns true if any element satisfies a given criterion; false otherwise.

If self has no element, returns false and argument or block are not used; otherwise behaves as below.

With no argument and no block, returns true if self is non-empty, false otherwise.

With argument entry and no block, returns true if for any key key self.assoc(key) == entry, false otherwise:

Example
h = {foo: 0, bar: 1, baz: 2}
h.assoc(:bar)     # => [:bar, 1]
h.any?([:bar, 1]) # => true
h.any?([:bar, 0]) # => false

With no argument and a block given, calls the block with each key-value pair; returns true if the block returns a truthy value, false otherwise:

Example
h = {foo: 0, bar: 1, baz: 2}
h.any? {|key, value| value < 3 } # => true
h.any? {|key, value| value > 3 } # => false

With both argument entry and a block given, issues a warning and ignores the block.

Related: Enumerable#any? (which this method overrides); see also Methods for Fetching.

See also #[]= #assoc #[] #clear also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0