Module: TCOMethod::Mixin

Defined in:
lib/tco_method/mixin.rb

Overview

Mixin providing tail call optimization eval and class annotations. When extended by a Class or Module adds methods for evaluating code with tail call optimization enabled and re-evaluating existing methods with tail call optimization enabled.

Instance Method Summary collapse

Instance Method Details

#tco_eval(code) ⇒ Object

Evaluate the given code String with tail call optimization enabled.

Parameters:

  • code (String)

    The code to evaluate with tail call optimization enabled.

Returns:

  • (Object)

    Returns the value of the final expression of the provided code String.

Raises:

  • (ArgumentError)

    if the provided code argument is not a String.

See Also:



30
31
32
# File 'lib/tco_method/mixin.rb', line 30

def tco_eval(code)
  TCOMethod.tco_eval(code)
end

#tco_method(method_name) ⇒ Symbol

Class annotation causing the instance method identified by the given method name to be reevaluated with tail call optimization enabled. Only works for methods defined using the `def` keyword.

Parameters:

  • method_name (String, Symbol)

    The name of the instance method that should be reeevaluated with tail call optimization enabled.

Returns:

  • (Symbol)

    The symbolized method name.

See Also:



42
43
44
# File 'lib/tco_method/mixin.rb', line 42

def tco_method(method_name)
  MethodReevaluator.new(self, method_name, :instance)
end

#tco_module_method(method_name) ⇒ Symbol Also known as: tco_class_method

Module or Class annotation causing the class or module method identified by the given method name to be reevaluated with tail call optimization enabled. Only works for methods defined using the `def` keyword.

Parameters:

  • method_name (String, Symbol)

    The name of the class or module method that should be reeevaluated with tail call optimization enabled.

Returns:

  • (Symbol)

    The symbolized method name.

See Also:



17
18
19
# File 'lib/tco_method/mixin.rb', line 17

def tco_module_method(method_name)
  MethodReevaluator.new(self, method_name, :module)
end

#with_tco(&block) ⇒ Object

Allows for executing a block of code with tail call optimization enabled.

All code that is evaluated in the block will be evaluated with tail call optimization enabled, however here be dragons, so make sure to read the docs for TCOMethod.with_tco before getting too crazy.

Parameters:

  • block (Proc)

    The proc to evaluate with tail call optimization enabled.

Returns:

  • (Object)

    Returns whatever the result of evaluating the given block.

See Also:



56
57
58
# File 'lib/tco_method/mixin.rb', line 56

def with_tco(&block)
  TCOMethod.with_tco(&block)
end