From: "David A. Black" Date: 2005-07-26T02:25:55+09:00 Subject: Re: Newby question 'unless' Hi -- On Tue, 26 Jul 2005, Patrick Gundlach wrote: > Hi, > >> def to_s >> "#{@a}" + "#{@b}" unless @a == 0 >> end > > def to_s > "#{@a}" + (@a == 0 ? "" : "#{@b}" ) > end Conditionals give you nil when they're not met, and nil.to_s is an empty string, so you don't have to specify a literal empty string in cases like this. I would refactor your second line to: "#{@a}" + ("#{@b}" unless @a.zero?) at which point you can make it nicer by getting rid of the parens and the + : "#{@a}#{@b unless @a.zero?}" That gives you a closer lexical mapping between what you're asking for and how the eventually string will read ("@a@b?", one might say). David -- David A. Black dblack@wobblini.net