Newby question 'unless'

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

You could try the ternary operator:

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

Or,

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

···

On 7/25/05, EdUarDo <eduardo.yanezNOSPAM@nospamgmail.com> 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.

What's the correct syntax?

I know I could do:

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

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
dblack@wobblini.net

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

···

On Jul 25, 2005, at 10:15 AM, EdUarDo wrote:

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

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

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.

Hi --

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

···

On Tue, 26 Jul 2005, Patrick Gundlach wrote:

--
David A. Black
dblack@wobblini.net

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

Yes :slight_smile:

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

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

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

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

David

--
David A. Black
dblack@wobblini.net

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

martin

···

EdUarDo <eduardo.yanezNOSPAM@nospamgmail.com> wrote:

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

Yes :slight_smile:

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

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