# Newby question 'unless'

Discussion in 'Ruby' started by EdUarDo, Jul 25, 2005.

1. ### EdUarDoGuest

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.

What's the correct syntax?

I know I could do:

def to_s
s = "\n#{@post}"
if @post != :GK
s += "#{@side}"
end
s
end

EdUarDo, Jul 25, 2005

2. ### BelorionGuest

On 7/25/05, EdUarDo <> wrote:
> Hi all, I'm overriding to_s method and I want to
> print an attribute if a condition is fulfilled.
>=20
> def to_s
> "#{@a}" + "#{@b}" unless @a =3D=3D 0
> end
>=20
> That sentence doesn't print anything when @a =3D=3D 0 but what I want to =

do
> is to not print @b when @a =3D=3D 0.
>=20
> What's the correct syntax?
>=20
> I know I could do:
>=20
> def to_s
> s =3D "\n#{@post}"
> if @post !=3D :GK
> s +=3D "#{@side}"
> end
> s
> end

You could try the ternary operator:

def to_s
@a =3D=3D 0 ? "#{@a}" : "#{@a} #{@b}"
end

Or,

def to_s
s =3D @a
s << " #{@b}" unless @a =3D=3D 0
return s
end
end

Belorion, Jul 25, 2005

3. ### Patrick GundlachGuest

Hi,

> def to_s
> "#{@a}" + "#{@b}" unless @a == 0
> end

def to_s
"#{@a}" + (@a == 0 ? "" : "#{@b}" )
end

> def to_s
> s = "\n#{@post}"
> if @post != :GK
> s += "#{@side}"

s += creates a new object. Doing << on strings is much faster.

Patrick

Patrick Gundlach, Jul 25, 2005
4. ### David A. BlackGuest

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

David

--
David A. Black

David A. Black, Jul 25, 2005
5. ### David A. BlackGuest

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

David A. Black, Jul 25, 2005
6. ### James Edward Gray IIGuest

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

James Edward Gray II, Jul 25, 2005
7. ### EdUarDoGuest

> def to_s
> s = @a
> s << " #{@b}" unless @a == 0
> return s
> end

I'll take this one, in this case the ternary operator
introduces a duplicity in the code that I don't like.

EdUarDo, Jul 25, 2005
8. ### EdUarDoGuest

> I think I figured out what you want You always want @a, and you
> only want @b if @a is not zero:

Yes

>
> "#{@a}#{@b unless @a.zero?}"

That's what I wanted. I didn't know that the condition could be into
the brackets. Thanks.

EdUarDo, Jul 25, 2005
9. ### David A. BlackGuest

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

David A. Black, Jul 25, 2005
10. ### Patrick GundlachGuest

Hello David,

> 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.

Sure, but not in the case I posted:

> def to_s
> "#{@a}" + (@a == 0 ? "" : "#{@b}" )
> end

-:4:in `+': cannot convert nil into String (TypeError)
from -:4:in `to_s'
from -:8

> "#{@a}#{@b unless @a.zero?}"

This is indeed much better. Thanks for your advice - I can see my ruby
skills getting better every day.

Patrick

Patrick Gundlach, Jul 25, 2005
11. ### David A. BlackGuest

Hi --

On Tue, 26 Jul 2005, Patrick Gundlach wrote:

> Hello David,
>
>> 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.

>
> Sure, but not in the case I posted:
>
>> def to_s
>> "#{@a}" + (@a == 0 ? "" : "#{@b}" )
>> end

>
>
> -:4:in `+': cannot convert nil into String (TypeError)

Whoops, I thought I was being helpful by showing an intermediate step
but indeed that + nil would not work

David

--
David A. Black

David A. Black, Jul 25, 2005
12. ### Martin DeMelloGuest

EdUarDo <> wrote:
> > I think I figured out what you want You always want @a, and you
> > only want @b if @a is not zero:

>
> Yes
>
> >
> > "#{@a}#{@b unless @a.zero?}"

>
> That's what I wanted. I didn't know that the condition could be into
> the brackets. Thanks.

#{} can contain an arbitrarily complex expression, the result of which
has to_s called on it before being interpolated.

martin

Martin DeMello, Jul 26, 2005