Commonly used Ruby utility functions related to reflection.
- get_bound_instance_method(instance:, method_name:)
- call(callback, params = nil)
- has_parameter?(proc, parameter_index, parameter)
- has_parameters?(proc, parameters)
- assert_parameters(proc, parameters)
- module CreateClassMethodsUponInclude (more: http://lifeinacubicleblog.com/2017/06/26/ruby-how-to-use-self-included-meaningfully)
- module CreateModuleFunctions (more: https://stackoverflow.com/questions/322470/can-i-invoke-an-instance-method-on-a-ruby-module-without-including-it)
For usage and examples for each of them take a look at the unit tests.
Another approach would be to include this functionality into classes where its needed. It may be more elegant however moving this functionality into a separate utility class decouples the logic and removes dependency from those classes.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install reflection_utils
require 'reflection_utils' # ... ReflectionUtils.get_bound_instance_method(instance: my_obj, method_name: :my_method) # etc.
Any feedback is much appreciated.
I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.
Please let me know if you make use of this project so that I can prioritize further efforts.
This gem is developed using Bundler conventions. A good overview can be found here.
Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/reflection_utils.
The gem is available as open source under the terms of the MIT License.