Skip to content

Instantly share code, notes, and snippets.

@luxerama
Last active December 23, 2015 12:39
Show Gist options
  • Save luxerama/6636289 to your computer and use it in GitHub Desktop.
Save luxerama/6636289 to your computer and use it in GitHub Desktop.
class Proc
def callback(callable, *args)
self === Class.new do
method_name = callable.to_sym
define_method(method_name) { |&block| block.nil? ? true : block.call(*args) }
define_method("#{method_name}?") { true }
def method_missing(method_name, *args, &block) false; end
end.new
end
end
publish "some content" do |receiver|
receiver.success do
puts 'published'
end
receiver.failure do |failure|
puts "not published because #{failure}"
end
end
def publish(content, &block)
display content
block.callback :success
rescue => e
block.callback :failure, e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment