Created
May 25, 2018 10:52
-
-
Save porras/ae2ba4a886e3098977f644abef1f260f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RespondTo | |
def initialize(method_name) | |
@method_name = method_name | |
end | |
def ===(object) | |
object.respond_to?(@method_name) | |
end | |
end | |
def RespondTo(method_name) | |
::RespondTo.new(method_name) | |
end | |
[ | |
1, | |
"string", | |
%w{an array}, | |
{a: 'hash'} | |
].each do |object| | |
case object | |
when String, Integer | |
puts object | |
when RespondTo(:join) | |
puts object.join(' ') | |
else | |
puts object.inspect | |
end | |
end | |
# Prints: | |
# 1 | |
# string | |
# an array | |
# {:a=>"hash"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment