Class: IRB::Kit::Handlers::Searcher

Inherits:
Abstract
  • Object
show all
Defined in:
lib/irb/kit/handlers/searcher.rb

Overview

Handles searching an object’s methods by pattern.

Instance Method Summary collapse

Constructor Details

#initialize(matcher: Regexp) ⇒ Searcher

Returns a new instance of Searcher.



8
9
10
11
# File 'lib/irb/kit/handlers/searcher.rb', line 8

def initialize(matcher: Regexp, **)
  @matcher = matcher
  super(**)
end

Instance Method Details

#call(object, pattern) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/irb/kit/handlers/searcher.rb', line 13

def call object, pattern
  object.methods
        .grep(matcher.new(pattern))
        .join("\n")
        .then { |result| result.empty? ? io.puts("No matches found.") : io.puts(result) }
rescue TypeError
  io.puts "ERROR: Use only a string or regular expression for the pattern."
end