Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=> "#<Object:0x007f802b8b92c0>"
>> raise "something went wrong..."
Expand Down
5 changes: 5 additions & 0 deletions lib/canada.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
module Canada
module ObjectExtensions
EH_METHOD_REGEXP = /\A(?<method_name>.+)_eh\?\z/
PLEASE_METHOD_REGEXP = /\A(?<method_name>.+)_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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/canada_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[],
Expand Down