0.0
No commit activity in last 3 years
No release in over 3 years
Substitute an existing method with a Proc. The Proc will have access to the replaced method and its parameters.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Substituter

Substituter is designed to easily substitute an existing class instance method with a provided Proc. The existing method is renamed and a reference is kept to this original method so that the new Proc that is executed will be able to call the original method and has access to its parameters.

Installation

Add this line to your application's Gemfile:

gem 'substituter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install substituter

Usage

my_proc = Proc.new{|original_method,*args|
  "My proc info and original methods value = #{original_method.call(*args)}"
}

Substituter.sub String, :to_s, my_proc
puts String.new("So Cool!").to_s

#-> "My proc info and original methods value = So Cool!" 

#to see a list of all substituted methods
Substituter.ls

#-> {"String" => [:to_s]}

#to restore the original method just clear the substitute
Substituter.clear String, :to_s

*args gottcha's

Generally calling original_method.call(*args) will work unless args contains a Proc object, which it will if the orginal method is called with a proc parameter or a block.

Note: an implicit block will be turned into a Proc object and appended to the args array.

Proc objects must be passed with the & operator prepended manually.

# our sample class with a method to substitute
class Sample
  def taking_a_param_and_block(param)
    block_given? yield(param) : param
  end
end

# our proc we will substitute in for the original method
my_proc = Proc.new{|original_method,*args|
  #here we explicitly pass in the arguments, making sure to add '&' to the proc object
  "My proc info and original methods value = #{original_method.call(args[0], &args[1])}"
}

Substituter.sub Sample, :taking_a_param_and_block, my_proc

Sample.new.taking_a_param_and_block("Hello") do |param|
  "#{param} World"
end

#-> "My proc info and original methods value = Hello World!" 

#see the spec/substituter_spec.rb for examples

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request