Class: Method

Inherits:
Object show all
Defined in:
lib/mug/apply.rb,
lib/mug/iterator/method.rb

Instance Method Summary collapse

Instance Method Details

#apply(*args) ⇒ Object, Proc

Note:

Method#curry is in stdlib since Ruby 2.2. apply is shorthand for curry.call(...).

Curries this Method and partially applies parameters. If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc that takes the rest of arguments.

Parameters:

  • args (Array)

    the arguments to partially apply

Returns:

  • (Object, Proc)

    the result if sufficient args, or a curried Proc



58
59
60
61
# File 'lib/mug/apply.rb', line 58

def apply(*args)
  n = arity < 0 ? -arity - 1 : arity
  curry(n).call(*args)
end

#to_iter(*args) ⇒ Iterator

Creates an Iterator object, which is a subclass of Enumerator that recursively invokes this method on an object.

Initially the receiving object is the object on which this method is defined. After each iteration, the receiving object is replaced with the result of the previous iteration.

Examples:

require 'mug/iterator/method'
0.method(:next).to_iter.take(5)    #=> [1, 2, 3, 4, 5]
0.method(:+).to_iter(2).take(5)    #=> [2, 4, 6, 8, 10]

Parameters:

  • args (Array)

    additional arguments passed to the method

Returns:

  • (Iterator)

    a new Iterator instance



21
22
23
24
25
# File 'lib/mug/iterator/method.rb', line 21

def to_iter *args
  Iterator.new(receiver) do |o|
    o.send(name, *args)
  end
end