Class: Lich::Common::Watchfor
- Inherits:
-
Object
- Object
- Lich::Common::Watchfor
- Defined in:
- lib/common/watchfor.rb
Overview
A class that watches for specific patterns in a script and executes a block of code when a match is found.
Class Method Summary collapse
-
.clear ⇒ nil
Clears all watchfor patterns from the current script.
Instance Method Summary collapse
-
#initialize(line, theproc = nil, &block) ⇒ nil
constructor
Initializes a new Watchfor instance.
Constructor Details
#initialize(line, theproc = nil, &block) ⇒ nil
Note:
This method disables a Rubocop linting rule regarding returning nil in a void context.
Initializes a new Watchfor instance.
rubocop:disable Lint/ReturnInVoidContext
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/common/watchfor.rb', line 21 def initialize(line, theproc = nil, &block) return nil unless (script = Script.current) if line.class == String line = Regexp.new(Regexp.escape(line)) elsif line.class != 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
.clear ⇒ nil
Note:
This method resets the watchfor hash to a new empty hash.
Clears all watchfor patterns from the current script.
50 51 52 |
# File 'lib/common/watchfor.rb', line 50 def Watchfor.clear script.watchfor = Hash.new end |