Skip to content

Instantly share code, notes, and snippets.

@threeifbywhiskey
Created March 7, 2014 22:37
Show Gist options
  • Save threeifbywhiskey/9421667 to your computer and use it in GitHub Desktop.
Save threeifbywhiskey/9421667 to your computer and use it in GitHub Desktop.
In which an object responds to its printer five different ways.
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
Copy link

djberg96 commented Mar 7, 2014

require 'pp'

class Thing
  def pretty_print(q)
    q.text("pretty print")
  end
end

pp Thing.new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment