Module: Tins::CasePredicate

Included in:
Object
Defined in:
lib/tins/case_predicate.rb

Overview

A module that provides a predicate method for checking if a value matches any of the given cases.

Instance Method Summary collapse

Instance Method Details

#case?(*args) ⇒ Object?

Checks if the object matches any of the given arguments using the === operator.

This method provides pattern matching functionality similar to Ruby’s case/when statements, using the === operator for semantic equality checking.

found

Examples:

Basic type matching

"hello".case?(String, Integer)     # => String (matches first argument)
42.case?(String, Integer)          # => Integer (matches first argument)
nil.case?(String, Integer)         # => nil (no matches)

Range and pattern matching

15.case?(1..10, 11..20, 21..30)    # => 11..20 (matches range)
"hello world".case?(/foo/, /hello/) # => /hello/ (matches regex)

Parameters:

  • args (Array)

    the arguments to check against using === operator

Returns:

  • (Object, nil)

    the first matching argument, or nil if no match is



24
25
26
# File 'lib/tins/case_predicate.rb', line 24

def case?(*args)
  args.find { |a| a === self }
end