Class: Lich::Common::Watchfor

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

Overview

Represents a watcher for specific patterns in the script. This class allows you to define patterns to watch for and execute a block of code when those patterns are matched.

Examples:

Creating a watcher for a specific string

watcher = Lich::Common::Watchfor.new("example string") { puts "Matched!" }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, theproc = nil, &block) ⇒ nil

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

Examples:

Initializing with a string

watcher = Lich::Common::Watchfor.new("test") { puts "Found!" }

Parameters:

  • line (String, Regexp)

    The string or regular expression to watch for.

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

    An optional Proc to execute if no block is given.

  • block (Proc)

    The block of code to execute when the pattern is matched.

Raises:

  • (ArgumentError)

    Raises an error if neither a string nor a regexp is provided.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'documented/common/watchfor.rb', line 21

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

Class Method Details

.clearvoid

This method returns an undefined value.

rubocop:enable Lint/ReturnInVoidContext Clears all watch patterns from the current script.

Examples:

Clearing all watchers

Lich::Common::Watchfor.clear


46
47
48
# File 'documented/common/watchfor.rb', line 46

def Watchfor.clear
  script.watchfor = Hash.new
end