# Newby question 'unless'

**URL:** <https://rubytalk.org/t/newby-question-unless/19729>\
**Category:** ruby-talk\
**Created:** [25 July 2005 15:15 UTC](https://rubytalk.org/t/newby-question-unless/19729 "2005-07-25T15:15:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![EdUarDo](https://avatars.discourse-cdn.com/v4/letter/e/6a8cbe/32.png) [@EdUarDo](https://rubytalk.org/u/EdUarDo)\
**Post date:** [25 July 2005 15:15 UTC](https://rubytalk.org/t/newby-question-unless/19729/1 "2005-07-25T15:15:59Z")

</div>

Hi all, I'm overriding to\_s method and I want to  
print an attribute if a condition is fulfilled.

def to\_s  
&nbsp;&nbsp;&nbsp;"#{@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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = "\n#{@post}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @post != :GK  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += "#{@side}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Belorion](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@Belorion](https://rubytalk.org/u/Belorion)\
**Post date:** [25 July 2005 15:22 UTC](https://rubytalk.org/t/newby-question-unless/19729/2 "2005-07-25T15:22:26Z")

</div>

You could try the ternary operator:

def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;@a == 0 ? "#{@a}" : "#{@a} #{@b}"  
end

Or,

def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;s = @a  
&nbsp;&nbsp;&nbsp;&nbsp;s \<\< " #{@b}" unless @a == 0  
&nbsp;&nbsp;&nbsp;&nbsp;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  
> > &nbsp;&nbsp;&nbsp;"#{@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:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = "\n#{@post}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @post != :GK  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += "#{@side}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 July 2005 15:24 UTC](https://rubytalk.org/t/newby-question-unless/19729/3 "2005-07-25T15:24:00Z")

</div>

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:
> 
> &nbsp;&nbsp;&nbsp;def to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{@a unless @a.zero?}#{@b unless @b.zero?}"  
> &nbsp;&nbsp;&nbsp;end
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [25 July 2005 15:26 UTC](https://rubytalk.org/t/newby-question-unless/19729/4 "2005-07-25T15:26:44Z")

</div>

> Hi all, I'm overriding to\_s method and I want to  
> print an attribute if a condition is fulfilled.
> 
> def to\_s  
> &nbsp;&nbsp;"#{@a}" + "#{@b}" unless @a == 0  
> end

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

def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{@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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @a.zero?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@a.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{@a}#{@b}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

Hope that helps.

James Edward Gray II

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

---

<div class="post-metadata">

**Author:** ![Patrick\_Gundlach6](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@Patrick\_Gundlach6](https://rubytalk.org/u/Patrick_Gundlach6)\
**Post date:** [25 July 2005 16:05 UTC](https://rubytalk.org/t/newby-question-unless/19729/5 "2005-07-25T16:05:59Z")

</div>

Hi,

> def to\_s  
> &nbsp;&nbsp;&nbsp;"#{@a}" + "#{@b}" unless @a == 0  
> end

&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;"#{@a}" + (@a == 0 ? "" : "#{@b}" )  
&nbsp;&nbsp;end

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = "\n#{@post}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @post != :GK  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s += "#{@side}"

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

Patrick

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 July 2005 15:26 UTC](https://rubytalk.org/t/newby-question-unless/19729/6 "2005-07-25T15:26:13Z")

</div>

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  
> > &nbsp;&nbsp;&nbsp;"#{@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:
> 
> &nbsp;&nbsp;&nbsp;"#{@a}#{@b unless @a.zero?}"
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![EdUarDo](https://avatars.discourse-cdn.com/v4/letter/e/6a8cbe/32.png) [@EdUarDo](https://rubytalk.org/u/EdUarDo)\
**Post date:** [25 July 2005 15:30 UTC](https://rubytalk.org/t/newby-question-unless/19729/7 "2005-07-25T15:30:59Z")

</div>

> def to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;s = @a  
> &nbsp;&nbsp;&nbsp;&nbsp;s \<\< " #{@b}" unless @a == 0  
> &nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 July 2005 17:25 UTC](https://rubytalk.org/t/newby-question-unless/19729/8 "2005-07-25T17:25:55Z")

</div>

Hi --

> Hi,
> 
> > def to\_s  
> > &nbsp;&nbsp;&nbsp;"#{@a}" + "#{@b}" unless @a == 0  
> > end
> 
> def to\_s  
> &nbsp;&nbsp;"#{@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:

&nbsp;&nbsp;&nbsp;&nbsp;"#{@a}" + ("#{@b}" unless @a.zero?)

at which point you can make it nicer by getting rid of the parens and  
the + :

&nbsp;&nbsp;&nbsp;&nbsp;"#{@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

---

<div class="post-metadata">

**Author:** ![EdUarDo](https://avatars.discourse-cdn.com/v4/letter/e/6a8cbe/32.png) [@EdUarDo](https://rubytalk.org/u/EdUarDo)\
**Post date:** [25 July 2005 15:45 UTC](https://rubytalk.org/t/newby-question-unless/19729/9 "2005-07-25T15:45:59Z")

</div>

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

Yes 🙂

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

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

---

<div class="post-metadata">

**Author:** ![Patrick\_Gundlach6](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@Patrick\_Gundlach6](https://rubytalk.org/u/Patrick_Gundlach6)\
**Post date:** [25 July 2005 18:15 UTC](https://rubytalk.org/t/newby-question-unless/19729/10 "2005-07-25T18:15:58Z")

</div>

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  
> &nbsp;&nbsp;"#{@a}" + (@a == 0 ? "" : "#{@b}" )  
> end

-:4:in `+': cannot convert nil into String (TypeError)  
&nbsp;&nbsp;from -:4:in `to\_s'  
&nbsp;&nbsp;from -:8

> &nbsp;&nbsp;&nbsp;&nbsp;"#{@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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 July 2005 19:25 UTC](https://rubytalk.org/t/newby-question-unless/19729/11 "2005-07-25T19:25:58Z")

</div>

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  
> > > &nbsp;&nbsp;"#{@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  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [26 July 2005 06:15 UTC](https://rubytalk.org/t/newby-question-unless/19729/12 "2005-07-26T06:15:59Z")

</div>

#{} 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 🙂 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.
