Newby question 'unless'

E

EdUarDo

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
 
B

Belorion

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
 
P

Patrick Gundlach

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
 
D

David A. Black

Hi --

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
 
D

David A. Black

Hi --

Hi --



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
 
J

James Edward Gray II

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
 
E

EdUarDo

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

David A. Black

Hi --

Hi,


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
 
P

Patrick Gundlach

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
 
D

David A. Black

Hi --

Hello David,


Sure, but not in the case I posted:



-: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
 
M

Martin DeMello

EdUarDo said:
Yes :)


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top