Class: Dentaku::AST::StringFunctions::Mid

Inherits:
Base show all
Defined in:
lib/dentaku/ast/functions/string_functions.rb

Constant Summary

Constants inherited from Function

Function::DIG

Instance Method Summary collapse

Methods inherited from Base

#negative_argument_failure, #type

Methods inherited from Function

#dependencies, get, numeric, register, register_class, registry

Methods inherited from Node

arity, #dependencies, peek, precedence

Constructor Details

#initialize(*args) ⇒ Mid

Returns a new instance of Mid.



48
49
50
51
# File 'lib/dentaku/ast/functions/string_functions.rb', line 48

def initialize(*args)
  super
  @string, @offset, @length = *@args
end

Instance Method Details

#value(context = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dentaku/ast/functions/string_functions.rb', line 53

def value(context = {})
  string = @string.value(context).to_s
  offset = @offset.value(context)
  negative_argument_failure('MID', 'offset') if offset < 0
  length = @length.value(context)
  negative_argument_failure('MID') if length < 0
  string[offset - 1, length].to_s
end