From: Zach Dennis Date: 2003-11-27T04:01:52+09:00 Subject: Re: defining a 'puts' or a 'print' for a class Thanks again Mark! -----Original Message----- From: Mark J. Reed [mailto:markjreed@mail.com] Sent: Wednesday, November 26, 2003 1:57 PM To: ruby-talk ML Subject: Re: defining a 'puts' or a 'print' for a class On Thu, Nov 27, 2003 at 03:48:58AM +0900, Zach Dennis wrote: > But I would love to say: > > puts Email > > to get the same result. > > Any ideas? Define a 'to_s' method. irb(main):001:0> class Point irb(main):002:1> def initialize(x, y) irb(main):003:2> @x, @y = x, y irb(main):004:2> end irb(main):005:1> irb(main):006:1* def to_s irb(main):007:2> '(' + @x.to_s + ', ' + @y.to_s + ')' irb(main):008:2> end irb(main):009:1> end => nil irb(main):010:0> p = Point.new(3,4) => # irb(main):011:0> puts p (3, 4) => nil If you also want your custom string form to show up in the irb inspection (after the =>), define 'inspect', too: irb(main):011:0> class Point irb(main):012:1> def inspect; to_s; end irb(main):013:1> end => nil irb(main):014:0> p => (3, 4) irb(main):015:0> -Mark