Class: Lich::Common::StringProc
- Inherits:
-
Object
- Object
- Lich::Common::StringProc
- 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
-
#_dump(_d = nil) ⇒ String
private
Returns the normalized source string for serialization.
-
#call(*_a) ⇒ Object
Evaluates the wrapped source string within a new Proc context.
-
#class ⇒ Class
private
Returns the class of an empty Proc.
-
#initialize(string) ⇒ StringProc
constructor
Create a StringProc that wraps the provided source text and normalizes CRLF and CR newlines to LF.
-
#inspect ⇒ String
Returns a string representation of this StringProc instance.
-
#kind_of?(type) ⇒ Boolean
Determine whether an empty Proc is an instance of the given class or module.
-
#to_json(*args) ⇒ String
private
Converts this StringProc to a JSON-compatible representation as a Lich5 inline command.
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.
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.
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.
54 55 56 |
# File 'documented/common/class_exts/stringproc.rb', line 54 def call(*_a) proc { eval(@string) }.call end |
#class ⇒ Class
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.
37 38 39 |
# File 'documented/common/class_exts/stringproc.rb', line 37 def class Proc end |
#inspect ⇒ String
Returns a string representation of this StringProc instance.
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.
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.
87 88 89 |
# File 'documented/common/class_exts/stringproc.rb', line 87 def to_json(*args) ";e #{_dump}".to_json(args) end |