Skip to content

Instantly share code, notes, and snippets.

@porras
Created May 25, 2018 10:52
Show Gist options
  • Save porras/ae2ba4a886e3098977f644abef1f260f to your computer and use it in GitHub Desktop.
Save porras/ae2ba4a886e3098977f644abef1f260f to your computer and use it in GitHub Desktop.
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