diff --git a/README.md b/README.md index a14d032..0112c50 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ It's well known that we have [different conventions](http://programmers.stackexc => false >> [].respond_to_eh?(:empty_eh?) => true +>> str = 'You Hoser' +>> str.upcase_please +>> str +=> 'YOU HOSER' >> aboot Object.new => "#" >> raise "something went wrong..." diff --git a/lib/canada.rb b/lib/canada.rb index 80a1444..6a494bf 100644 --- a/lib/canada.rb +++ b/lib/canada.rb @@ -3,10 +3,13 @@ module Canada module ObjectExtensions EH_METHOD_REGEXP = /\A(?.+)_eh\?\z/ + PLEASE_METHOD_REGEXP = /\A(?.+)_please\z/ def respond_to_missing?(meth, include_all = false) if (m = EH_METHOD_REGEXP.match(meth)) super || self.respond_to?("#{m[:method_name]}?", include_all) + elsif (m = PLEASE_METHOD_REGEXP.match(meth)) + super || self.respond_to?("#{m[:method_name]}!", include_all) else super end @@ -15,6 +18,8 @@ def respond_to_missing?(meth, include_all = false) def method_missing(meth, *args, &block) if (m = EH_METHOD_REGEXP.match(meth)) self.public_send("#{m[:method_name]}?", *args, &block) + elsif (m = PLEASE_METHOD_REGEXP.match(meth)) + self.public_send("#{m[:method_name]}!", *args, &block) else super end diff --git a/test/canada_test.rb b/test/canada_test.rb index 9ceacc5..ff49140 100644 --- a/test/canada_test.rb +++ b/test/canada_test.rb @@ -23,6 +23,26 @@ def test_respond_to? no.each { |n| refute([].respond_to?(n), "Array should not respond_to_eh? #{n}") } end + def test_please + expectations = { + capitalize_please: ['good day', "Good day"], + chomp_please: ["Good Day\n", 'Good Day'], + chop_please: ['Good Day!', 'Good Day'], + downcase_please: ['Good Day', 'good day'], + lstrip_please: [' Good Day', 'Good Day'], + reverse_please: ['Good Day', 'yaD dooG'], + rstrip_please: ['Good Day ', 'Good Day'], + succ_please: ['Good Day', 'Good Daz'], + upcase_please: ['Good Day', 'GOOD DAY'] + } + + expectations.each do |m, (before, after)| + before.send m + assert(before == after, "String##{m} should equal #{after}") + assert(before.respond_to?(m), "String should respond_to? #{m}") + end + end + def test_aboot cases = [ [],