Skip to content

Instantly share code, notes, and snippets.

@wkjagt
Last active September 28, 2015 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkjagt/cf2794883664e9305be8 to your computer and use it in GitHub Desktop.
Save wkjagt/cf2794883664e9305be8 to your computer and use it in GitHub Desktop.

Is this a bad idea?

module Decorate
  def decorate(definition, options)	
		self.send(:alias_method, "__original_#{definition}", definition)
		define_method definition, Proc.new { |*args|
			send(options.fetch(:with), *args, &Proc.new { |args|
				send("__original_#{definition}", *args)
			})
		}
	end
end

Object.include Decorate

class A
	class B
	
		def double(a)
			a * 2
		end
		
		def around(a)
			# do stuff before
			yield(a)
			# do stuff after
		end
		decorate :double, with: :around
	end	
end

a = A::B.new

p a.double(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment