Created
May 3, 2012 05:14
-
-
Save phinze/2583389 to your computer and use it in GitHub Desktop.
simple dynamic dispatch example
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 DisinterestedCat | |
def speak | |
puts "..." | |
end | |
def play | |
puts "...<look away>" | |
end | |
end | |
fluffy = DisinterestedCat.new | |
fluffy.send(:speak) | |
# => "..." | |
fluffy.send("play".to_sym) | |
# => "...<look away>" | |
puts fluffy.respond_to?(:play) | |
# => true | |
puts fluffy.respond_to?(:sit) | |
# => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment