Class: Lich::Common::StringProc

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

Overview

Represents a String processing utility that evaluates a string as Ruby code. This class allows for the creation of callable objects that can execute Ruby code contained in a string.

Examples:

Creating a StringProc and calling it

proc = StringProc.new("1 + 1")
result = proc.call() # => 2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringProc

Initializes a new StringProc instance.

Parameters:

  • string (String)

    The string to be processed and evaluated.



15
16
17
# File 'documented/common/class_exts/stringproc.rb', line 15

def initialize(string)
  @string = string
end

Class Method Details

._load(string) ⇒ StringProc

Loads a StringProc object from a string representation.

Parameters:

  • string (String)

    The string to load into a StringProc.

Returns:

  • (StringProc)

    The newly created StringProc instance from the string.



69
70
71
# File 'documented/common/class_exts/stringproc.rb', line 69

def StringProc._load(string)
  StringProc.new(string)
end

Instance Method Details

#_dump(_d = nil) ⇒ String

Dumps the string representation of the object.

Parameters:

  • _d (nil) (defaults to: nil)

    Optional parameter, not used.

Returns:

  • (String)

    The string representation of the StringProc.



45
46
47
# File 'documented/common/class_exts/stringproc.rb', line 45

def _dump(_d = nil)
  @string
end

#call(*_a) ⇒ Object

Executes the Ruby code contained in the string.

Examples:

Calling the StringProc

result = proc.call() # => evaluates the string and returns the result

Returns:

  • (Object)

    The result of the evaluated string.



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

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

#classClass

Returns the class of the object.

Returns:

  • (Class)

    The class of the object, which is Proc.



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

def class
  Proc
end

#inspectString

Returns a string representation of the StringProc object.

Examples:

Inspecting the StringProc

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

Returns:

  • (String)

    A string that describes the StringProc instance.



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

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

#kind_of?(type) ⇒ Boolean

Checks if the object is of a certain type.

Examples:

Checking the type

proc.kind_of?(Proc) # => true

Parameters:

  • type (Class)

    The class to check against.

Returns:

  • (Boolean)

    Returns true if the object is of the specified type.



24
25
26
# File 'documented/common/class_exts/stringproc.rb', line 24

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

#to_json(*args) ⇒ String

Converts the StringProc object to JSON format.

Examples:

Converting to JSON

json = proc.to_json()

Parameters:

  • args (Array)

    Additional arguments for JSON conversion.

Returns:

  • (String)

    The JSON representation of the StringProc.



62
63
64
# File 'documented/common/class_exts/stringproc.rb', line 62

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