Class: Functio::Function
- Inherits:
-
Object
- Object
- Functio::Function
- Defined in:
- lib/functio/function.rb
Overview
Represents a function.
Constant Summary collapse
- MAX_NAME_SIZE =
Maximum size of the function name.
20- NAME_PATTERN =
Pattern for valid function name format.
/\A[a-zA-Z][a-zA-Z0-9]*\z/
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Expression instance used for Function definition.
-
#name ⇒ Object
readonly
Function name.
Class Method Summary collapse
-
.build(attributes) ⇒ Object
Builds a new Function instance by Function
attributes.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares equality between Function instances.
-
#attributes ⇒ Object
Function attributes.
-
#definition ⇒ Object
Function definition.
-
#initialize(args) ⇒ Function
constructor
Constructs a Function instance.
-
#params ⇒ Object
Returns the function input parameters in an Array of Strings.
-
#use(args = {}) ⇒ Object
Uses function with
argsHash.
Constructor Details
#initialize(args) ⇒ Function
Constructs a Function instance. The parameter args is a Hash containing the function :name and an :expression instance.
51 52 53 54 |
# File 'lib/functio/function.rb', line 51 def initialize(args) @name = validate_name(args[:name]) @expression = args[:expression] end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Expression instance used for Function definition.
36 37 38 |
# File 'lib/functio/function.rb', line 36 def expression @expression end |
#name ⇒ Object (readonly)
Function name.
33 34 35 |
# File 'lib/functio/function.rb', line 33 def name @name end |
Class Method Details
.build(attributes) ⇒ Object
Builds a new Function instance by Function attributes. attributes is a Hash containing :name and :definition. Raises InvalidFunctionError if some of the attributes are invalid. Raises DivisionByZeroError if :definition contains a division by zero.
42 43 44 45 46 47 |
# File 'lib/functio/function.rb', line 42 def self.build(attributes) expression = Expression.new(attributes[:definition]) new(name: attributes[:name], expression: expression) rescue InvalidExpressionError raise InvalidFunctionError, 'definition must be a valid expression' end |
Instance Method Details
#==(other) ⇒ Object
Compares equality between Function instances. Returns true if Function#attributes of this instance is equal to Function#attributes of other.
80 81 82 |
# File 'lib/functio/function.rb', line 80 def ==(other) attributes == other.attributes end |
#attributes ⇒ Object
Function attributes.
62 63 64 |
# File 'lib/functio/function.rb', line 62 def attributes { name: name, definition: definition } end |
#definition ⇒ Object
Function definition.
57 58 59 |
# File 'lib/functio/function.rb', line 57 def definition expression.expression end |
#params ⇒ Object
Returns the function input parameters in an Array of Strings.
67 68 69 |
# File 'lib/functio/function.rb', line 67 def params expression.variables end |
#use(args = {}) ⇒ Object
Uses function with args Hash. The keys of args Hash are the input parameters and the values are the input arguments.
73 74 75 |
# File 'lib/functio/function.rb', line 73 def use(args = {}) expression.evaluate(args) end |