Class: Lich::Common::Watchfor

Inherits:
Object
  • Object
show all
Defined in:
documented/common/watchfor.rb

Overview

Represents a watcher that triggers a block of code when a specified line matches a pattern.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(line, theproc = nil) { ... } ⇒ void

rubocop:disable Lint/ReturnInVoidContext Initializes a new Watchfor instance.

Parameters:

  • line (String, Regexp)

    the line to watch for

  • theproc (Proc, nil) (defaults to: nil)

    an optional proc to use if no block is given

Yields:

  • the block to execute when the line matches



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'documented/common/watchfor.rb', line 14

def initialize(line, theproc = nil, &block)
  return nil unless (script = Script.current)

  if line.is_a?(String)
    line = Regexp.new(Regexp.escape(line))
  elsif !line.is_a?(Regexp)
    echo 'watchfor: no string or regexp given'
    return nil
  end
  if block.nil?
    if theproc.respond_to? :call
      block = theproc
    else
      echo 'watchfor: no block or proc given'
      return nil
    end
  end
  script.watchfor[line] = block
end