Class: Lich::Common::StringProc

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

Overview

A wrapper around a Ruby source string that behaves like a Proc when evaluated.

StringProc stores and normalizes source text, converting all \r\n and \r newlines to \n, then delegates to a dynamically created Proc when called. It is used internally to serialize and deserialize script code in the Lich5 system.

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringProc

Create a StringProc that wraps the provided source text and normalizes CRLF and CR newlines to LF. The input is converted with to_s and stored with all \r\n and \r replaced by \n.

Parameters:

  • string (Object)
    • The source text to wrap; will be converted to a String and normalized.
  • string (String, #to_s)

    Ruby source text to evaluate when called.



21
22
23
# File 'documented/common/class_exts/stringproc.rb', line 21

def initialize(string)
  @string = string.to_s.gsub(/\r\n?/, "\n")
end

Instance Method Details

#_dump(_d = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the normalized source string for serialization.

The optional depth parameter is accepted but ignored; it is provided for compatibility with Ruby's marshaling protocol.

Parameters:

  • _d (Integer, nil) (defaults to: nil)

    depth parameter, ignored

Returns:

  • (String)

    the wrapped source text with all newlines normalized to \n



66
67
68
# File 'documented/common/class_exts/stringproc.rb', line 66

def _dump(_d = nil)
  @string
end

#call(*_a) ⇒ Object

Evaluates the wrapped source string within a new Proc context.

The source is eval'd inside a block, so it shares the binding of the current scope when called. Arguments passed to this method are accepted but ignored.

Examples:

sp = StringProc.new("1 + 2")
sp.call #=> 3

Parameters:

  • _a (Object)

    variable arguments, ignored

Returns:

  • (Object)

    the result of evaluating the wrapped source

Raises:

  • (SyntaxError)

    if the wrapped source contains invalid Ruby syntax

  • (NameError)

    if the source references an undefined variable or method

  • (StandardError)

    for other errors raised during eval



54
55
56
# File 'documented/common/class_exts/stringproc.rb', line 54

def call(*_a)
  proc { eval(@string) }.call
end

#classClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the class of an empty Proc.

Returns:

  • (Class)

    always Proc



37
38
39
# File 'documented/common/class_exts/stringproc.rb', line 37

def class
  Proc
end

#inspectString

Returns a string representation of this StringProc instance.

Examples:

StringProc.new("x = 1").inspect #=> "StringProc.new(\"x = 1\")"

Returns:

  • (String)

    a string in the format StringProc.new(...) with the wrapped source inspected



75
76
77
# File 'documented/common/class_exts/stringproc.rb', line 75

def inspect
  "StringProc.new(#{@string.inspect})"
end

#kind_of?(type) ⇒ Boolean

Determine whether an empty Proc is an instance of the given class or module.

Parameters:

  • type (Module)
    • The class or module to check against.

Returns:

  • (Boolean)

    true if a Proc is kind of type, false otherwise.



29
30
31
# File 'documented/common/class_exts/stringproc.rb', line 29

def kind_of?(type)
  Proc.new {}.kind_of? type
end

#to_json(*args) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts this StringProc to a JSON-compatible representation as a Lich5 inline command.

The source is serialized as a command string prefixed with ;e (inline eval), then converted to JSON. Additional arguments are passed to the String#to_json method.

Parameters:

  • args (Object)

    variable arguments forwarded to String#to_json

Returns:

  • (String)

    JSON-encoded representation of the ;e <source> command



87
88
89
# File 'documented/common/class_exts/stringproc.rb', line 87

def to_json(*args)
  ";e #{_dump}".to_json(args)
end