Created
March 7, 2014 22:37
-
-
Save threeifbywhiskey/9421667 to your computer and use it in GitHub Desktop.
In which an object responds to its printer five different ways.
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
require 'pp' | |
class Thing | |
def to_s | |
case caller[0] | |
when /write/ then 'print' | |
else 'puts' | |
end | |
end | |
def inspect | |
case caller[0] | |
when /pretty/ then 'pp' | |
else 'p' | |
end | |
end | |
def text arg | |
puts 'pretty_print' | |
end | |
end | |
t = Thing.new | |
p t | |
pp t | |
print t | |
pretty_print t | |
puts t |
djberg96
commented
Mar 7, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment