Installation

gem install instance_call

Why?

Introduces method Object#instance_call what takes Symbol, Proc or block and executes it in the context of instance.

Usage

require 'instance_call'

Examples:

  array   = [1, 2, 3]
  string  = "abc"
  array.instance_call(:last)                  # 3
  array.instance_call(:push, string)          # [1, 2, 3, "abc"]
  array.instance_call(lambda { [self, last]}) # [[1, 2, 3, "abc"], "abc"] - Note that self is not main
  array.instance_call() {|| self }            # [1, 2, 3, "abc"]

  array.instance_call(:'last.concat', 'd')    # [1, 2, 3, "abcd"]
  unbound_method = Array.instance_method(:pop)
  array.instance_call(unbound_method)         # "abcd"
  bound_method = array.method(:pop)
  array.instance_call(bound_method)           # 3

More examples can be seen in Rspec (tests)[https://github.com/tione/instance_call/blob/master/spec/lib/instance_call/core_ext/object_spec.rb]