On Jul 25, 2005, at 10:15 AM, 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

Just FYI, It's better just to include that in one string:

def to_s
     "#{@a}#{@b}" unless @a.zero?
end

> That sentence doesn't print anything when @a == 0 but what I want  
> to do
> is to not print @b when @a == 0.
>
> What's the correct syntax?

def to_s
     if @a.zero?
         @a.to_s
     else
         "#{@a}#{@b}"
     end
end

Hope that helps.

James Edward Gray II