Class: Code::Object::Function
- Inherits:
-
Code::Object
- Object
- Code::Object
- Code::Object::Function
- Defined in:
- lib/code/object/function.rb
Constant Summary
Constants inherited from Code::Object
Instance Attribute Summary collapse
-
#code_body ⇒ Object
readonly
Returns the value of attribute code_body.
-
#code_parameters ⇒ Object
readonly
Returns the value of attribute code_parameters.
Attributes inherited from Code::Object
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_call(*arguments, **globals) ⇒ Object
-
#initialize(*args, **_kargs) ⇒ Function
constructor
A new instance of Function.
- #signature_for_call ⇒ Object
Methods inherited from Code::Object
#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, code_different, #code_different, #code_equal_equal, code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs) ⇒ Function
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/code/object/function.rb', line 8 def initialize(*args, **_kargs, &) @code_parameters = List .new(args.first) .raw .map { |parameter| Parameter.new(parameter) } .to_code @code_body = Code.new(args.second.presence) @raw = List.new([code_parameters, code_body]) end |
Instance Attribute Details
#code_body ⇒ Object (readonly)
Returns the value of attribute code_body.
6 7 8 |
# File 'lib/code/object/function.rb', line 6 def code_body @code_body end |
#code_parameters ⇒ Object (readonly)
Returns the value of attribute code_parameters.
6 7 8 |
# File 'lib/code/object/function.rb', line 6 def code_parameters @code_parameters end |
Instance Method Details
#call(**args) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/code/object/function.rb', line 21 def call(**args) code_operator = args.fetch(:operator, nil).to_code code_arguments = args.fetch(:arguments, List.new).to_code globals = multi_fetch(args, *GLOBALS) case code_operator.to_s when "", "call" sig(args) { signature_for_call } code_call(*code_arguments.raw, **globals) else super end end |
#code_call(*arguments, **globals) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/code/object/function.rb', line 35 def code_call(*arguments, **globals) code_arguments = arguments.to_code code_context = Context.new({}, globals[:context]) code_parameters.raw.each.with_index do |code_parameter, index| code_argument = if code_parameter.keyword? code_arguments .raw .select { |code_argument| code_argument.is_a?(Dictionary) } .detect do |code_dictionary| code_dictionary.code_has_value?(parameter.code_name).truthy? end &.code_get(parameter.code_name) else code_arguments.raw[index].to_code end if code_argument.nothing? code_argument = code_parameter.code_evaluate(**globals) end code_context.code_set(code_parameter.code_name, code_argument) end code_body.code_evaluate(**globals, context: code_context) end |
#signature_for_call ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/code/object/function.rb', line 63 def signature_for_call code_parameters .raw .inject([]) do |signature, code_parameter| if code_parameter.keyword? if signature.last.is_a?(::Hash) signature.last[code_parameter.code_name] = Object signature else signature + [{ code_parameter.code_name => Object }] end else signature + [Object] end end + [Object.repeat] end |