Skip to content

Instantly share code, notes, and snippets.

@sbeam
Last active August 29, 2015 14:06
Show Gist options
  • Save sbeam/7e0dd95453d1e54398b7 to your computer and use it in GitHub Desktop.
Save sbeam/7e0dd95453d1e54398b7 to your computer and use it in GitHub Desktop.
wrapping snazziness
class Foo
def self.with_enthusiasm
boring_old_methods = instance_methods(false)
yield
snazzy_new_methods = instance_methods(false) - boring_old_methods
snazzy_new_methods.each do |method|
method_without_chains = "#{method.to_s}_with_reset".to_sym
alias_method method_without_chains, method
define_method method do |*args|
puts "\033[31m<POW>"
self.send method_without_chains, *args
puts "<SMASH>\033[0m"
end
end
end
def lame
puts 'lame.'
end
def blah
puts 'boring.'
end
with_enthusiasm do
def foo n
n.times do
puts 'yay!'
end
end
def bar n
n.times do
puts 'huzzah!'
end
end
end
end
f = Foo.new
puts f.foo 2
puts f.blah
puts f.lame
puts f.bar 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment