From: "David A. Black" Date: 2005-07-26T00:26:13+09:00 Subject: Re: Newby question 'unless' Hi -- On Tue, 26 Jul 2005, David A. Black wrote: > Hi -- > > On Tue, 26 Jul 2005, EdUarDo wrote: > >> Hi all, I'm overriding to_s method and I want to >> print an attribute if a condition is fulfilled. >> >> def to_s >> "#{@a}" + "#{@b}" unless @a == 0 >> end >> >> That sentence doesn't print anything when @a == 0 but what I want to do >> is to not print @b when @a == 0. > > If it doesn't print anything then presumably it's not printing @b. Or > did you mean that last @a to be @b? If so, you could do: > > def to_s > "#{@a unless @a.zero?}#{@b unless @b.zero?}" > end I think I figured out what you want :-) You always want @a, and you only want @b if @a is not zero: "#{@a}#{@b unless @a.zero?}" David -- David A. Black dblack@wobblini.net