From 8a7ac08ebd71fa9ccdebbb175ada61731c36ff03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rory=20O=E2=80=99Kane?= Date: Tue, 7 Oct 2014 02:49:37 -0400 Subject: [PATCH] Implement `respond_to?` in module Monad MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prompted by the statement “if this was production code, we should remember to implement `#respond_to?` as well” at http://codon.com/refactoring-ruby-with-monads. I implemented `respond_to?` by implementing `respond_to_missing?`, as recommended by http://robots.thoughtbot.com/always-define-respond-to-missing-when-overriding, so that `method` also works. This requires Ruby 1.9 and later. --- lib/monads/monad.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/monads/monad.rb b/lib/monads/monad.rb index 26e0bbd..30672f6 100644 --- a/lib/monads/monad.rb +++ b/lib/monads/monad.rb @@ -12,6 +12,12 @@ def method_missing(*args, &block) end end + def respond_to_missing?(method_name, include_private=false) + super || within do |value| + value.respond_to?(method_name, include_private) + end + end + private def ensure_monadic_result(&block)