Skip to content

Instantly share code, notes, and snippets.

@zeisler
Last active February 2, 2018 00:11
Show Gist options
  • Save zeisler/1470ca53344e3a93e72c7af980bb19d0 to your computer and use it in GitHub Desktop.
Save zeisler/1470ca53344e3a93e72c7af980bb19d0 to your computer and use it in GitHub Desktop.
Converts Object.new(<args>).call to Object.call(<args>)
require "reverse_parameters"
# Converts Object.new(<args>).call to Object.call(<args>)
# module must be extend after initialize method in order to transfer parameters
module CallObject
def create_callable
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def self.call(#{ReverseParameters.new(instance_method(:initialize)).parameters})
self.new(#{ReverseParameters.new(instance_method(:initialize)).arguments}).call
end
RUBY
end
def self.extended(base)
base.create_callable
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment