Sha256: b429ffd8bc760f87530a7e7c10d4a43eeb3f8ffd605b1471bda38cf16d444fd5

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

module CasePredicates
  # IsA includes predicate methods for type checks
  #
  # @example
  #   v = "foo"
  #   case v
  #   when string?
  #     puts "it's a string"
  #   else
  #     raise "on no!"
  #   end
  module IsA
    TYPES = {
      array: Array,
      complex: Complex,
      dir: Dir,
      enumerable: Enumerable,
      false => FalseClass,
      fiber: Fiber,
      file: File,
      float: Float,
      hash: Hash,
      io: IO,
      integer: Integer,
      module: Module,
      mutex: Mutex,
      nil: NilClass,
      numeric: Numeric,
      object: Object,
      proc: Proc,
      queue: Queue,
      random: Random,
      range: Range,
      rational: Rational,
      regexp: Regexp,
      string: String,
      struct: Struct,
      symbol: Symbol,
      thread: Thread,
      time: Time,
      true => TrueClass
    }.freeze

    TYPES.each do |type, klass|
      define_method(:"#{type}?") do
        ->(v) { v.is_a? klass }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
case_predicates-0.1.2 lib/case_predicates/is_a.rb
case_predicates-0.1.1 lib/case_predicates/is_a.rb