Class: Symbol

Inherits:
Object show all
Defined in:
lib/patch/foobar.rb

Overview

Extends the Symbol class for functional programming patterns.

Instance Method Summary collapse

Instance Method Details

#with(*args, &block) ⇒ Proc

Creates a Proc that functions as a curried instance method for objects It is possible to supply a block (to the instance method) for later use. This method allows for partial application of instance methods.

Examples:

plus_2 = :+.with(2)
plus_2.call(5) # => 7

mapper = :map.with { |x| x * x }
data = [1, 2, 3]
mapper.call(data) # => [1, 4, 9]

# Combining with map
[1, 2, 3].map(&:+.with(10)) # => [11, 12, 13]

Parameters:

  • `args` (Array<Object>)

    Arguments to be pre-supplied to the method.

  • `block` (Proc)

    A block to be pre-supplied to the method.

Returns:

  • (Proc)

    A lambda that expects a receiver object and any further arguments.



315
316
317
# File 'lib/patch/foobar.rb', line 315

def with(*args, &block)
  ->(o, *more) { o.send(self, *args, *more, &block) }
end