Module: Interrogate

Defined in:
lib/interrogate.rb,
lib/interrogate/version.rb

Overview

String?(“Hello”) Interrogate attempts to bring Scheme-like class predication to Ruby. It provides an alternate syntax using Module#===

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.interrogate(meth, *args, &block) ⇒ Object

Given an interrogatory method name and arguments, determine which class we are asking about and ask each arg if it is an instance of this class.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
# File 'lib/interrogate.rb', line 12

def self.interrogate(meth, *args, &block)
  raise ArgumentError, "#{meth}: requires at least one argument and/or a block" if args.length < 1 && !block

  klass_name = meth.to_s[0..-2]
  klass = Object.const_get(klass_name)
  args << yield if block_given?
  args.inject(true) { |bool, obj| bool && klass === obj }
end

.is_interrogatory?(meth) ⇒ Boolean

Determine if the given method matches an interrogatory method name.

Returns:

  • (Boolean)


23
24
25
# File 'lib/interrogate.rb', line 23

def self.is_interrogatory?(meth)
  /^[A-Z].*\?$/ === meth.to_s
end