Class: Object

Inherits:
BasicObject
Includes:
Full, Tins::AskAndSend, Tins::Attempt, Tins::BlockSelf, Tins::CasePredicate, Tins::Complete, Tins::DeepDup, Tins::Deflect, Tins::Eigenclass, Tins::Expose, Tins::IfPredicate, Tins::Interpreter, Tins::Null::Kernel, Tins::P, Tins::RequireMaybe, Tins::Responding, Tins::TempIO, Tins::ThreadGlobal, Tins::ThreadLocal, Tins::To
Defined in:
lib/tins/xt/ask_and_send.rb,
lib/tins/xt/attempt.rb,
lib/tins/xt/case_predicate.rb,
lib/tins/xt/complete.rb,
lib/tins/xt/deep_dup.rb,
lib/tins/xt/dslkit.rb,
lib/tins/xt/expose.rb,
lib/tins/xt/full.rb,
lib/tins/xt/if_predicate.rb,
lib/tins/xt/named.rb,
lib/tins/xt/null.rb,
lib/tins/xt/p.rb,
lib/tins/xt/require_maybe.rb,
lib/tins/xt/responding.rb,
lib/tins/xt/temp_io.rb,
lib/tins/xt/to.rb

Instance Method Summary collapse

Methods included from Tins::To

#to

Methods included from Tins::TempIO

#temp_io

Methods included from Tins::Responding

#responding?

Methods included from Tins::RequireMaybe

#require_maybe

Methods included from Tins::P

#p!, #pp!

Methods included from Tins::Null::Kernel

#null, #null_plus

Methods included from Tins::IfPredicate

#if?

Methods included from Tins::Expose

#expose

Methods included from Tins::BlockSelf

#block_self, block_self

Methods included from Tins::Eigenclass

#eigenclass_class_eval, #eigenclass_eval

Methods included from Tins::Deflect

#deflect, deflect?, #deflect?, #deflect_start, #deflect_stop

Methods included from Tins::ThreadLocal

#instance_thread_local, #thread_local

Methods included from Tins::Interpreter

#interpret, #interpret_with_binding

Methods included from Tins::ThreadGlobal

#instance_thread_global, #thread_global

Methods included from Tins::DeepDup

#deep_dup

Methods included from Tins::Complete

#complete, complete

Methods included from Tins::CasePredicate

#case?

Methods included from Tins::Attempt

#attempt, #compute_duration_base, #interpret_sleep, #sleep_duration

Methods included from Tins::AskAndSend

#ask_and_send, #ask_and_send!, #ask_and_send_or_self, #ask_and_send_or_self!

Instance Method Details

#named(name, method, *args) {|Object| ... } ⇒ Object

Adds a dynamically created method to the object instance. The method will call the specified method with optional args and combine any provided named_block with runtime blocks.

Examples:

Create a method that maps elements

a = [1, 2, 3]
a.named(:double, :map) { |x| x * 2 }
a.double  # => [2, 4, 6]

Pre-bind arguments to a method

def process(data, multiplier, &block)
  data.map { |x| block.call(x * multiplier) }
end

Object.named(:process_by_10, :process, 10) do |result|
  result + 1
end
process_by_10([1, 2, 3]) { |x| x * 2 }  # => [21, 41, 61]

Parameters:

  • name (Symbol)

    The name of the method to create

  • method (Symbol)

    The existing method to delegate to

  • args (Array)

    Optional arguments to pre-bind to the delegated method

Yields:

  • (Object)

    Optional block to be used as the method’s block

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tins/xt/named.rb', line 35

def named(name, method, *args, &named_block)
  name = name.to_sym
  m = Tins::Named.new {
    define_method(name) do |*rest, &block|
      block = named_block if named_block
      __send__(method, *(args + rest), &block)
    end
  }
  if m.respond_to?(:set_temporary_name)
    m.set_temporary_name "#{m.class} for method #{name.inspect}"
  end
  extend m
end