Module: Sinclair::MethodDefinition::InstanceMethodDefinition Private

Defined in:
lib/sinclair/method_definition/instance_method_definition.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Module responsible for building instance method definitions

Author:

  • darthjee

Class Method Summary collapse

Class Method Details

.from(name, code = nil, **options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an instance method definition

When block is given returns an instance of Sinclair::MethodDefinition::InstanceBlockDefinition, and when not, returns Sinclair::MethodDefinition::InstanceStringDefinition

Examples:

klass = Class.new

method_definition = Sinclair::MethodDefinition::InstanceMethodDefinition.from(
  :sequence, '@x = @x.to_i ** 2 + 1'
)

method_definition.build(klass)

instance = klass.new

instance.sequence # returns 1
instance.sequence # returns 2
instance.sequence # returns 5

Parameters:

  • name (String, Symbol)

    name of the method

  • code (String) (defaults to: nil)

    code to be evaluated as method

  • block (Proc)

    block with code to be added as method

  • options (Hash)

    Options of construction

Options Hash (**options):

  • cached (Boolean)

    Flag telling to create a method with cache

Returns:

  • MethodDefinition



39
40
41
42
43
44
45
# File 'lib/sinclair/method_definition/instance_method_definition.rb', line 39

def self.from(name, code = nil, **options, &block)
  if block
    InstanceBlockDefinition.new(name, **options, &block)
  else
    InstanceStringDefinition.new(name, code, **options)
  end
end