# Conditional replacements in rhtml

**URL:** <https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327>\
**Category:** Rails\
**Created:** [January 25, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327 "2006-01-25T19:58:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 25, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327/1 "2006-01-25T19:58:52Z")

</div>

On Wed, 25 Jan 2006 10:32:23 -0700, Ben R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> To do this with an if statement:
> 
> \<%if @items.size == 1 -%\>item\<% else -%\>items\<% end -%\>

Thanks you. Nonetheless I still remain uncertain why the following  
construct does not work:

> Your cart currently holds \<%= @items.size %\> \<%= if @items.size == 1 then "item" else "items" %\>.

If one considers only the issue of token parsing then surely the  
construct \<%= if @items.size == 1 ? “true” : “false” %\> is  
equivalent to \<%= if @items.size == 1 then “true” else “false” %\>.

It seems odd to me that the former works while the latter fails.  
If there is a syntax reason why this behaviour is expected then I  
would like to know what it is.

Regards,  
Jim

–  
\*\*\* e-mail is not a secure channel \*\*\*  
mailto:byrnejb.@harte-lyne.ca  
James B. Byrne Harte & Lyne Limited  
vox: +1 905 561 1241 9 Brockley Drive  
fax: +1 905 561 0757 Hamilton, Ontario  
 = hal Canada L8E 3C3

## ------- End of forwarded message -------

```
 ***e-mail is not a secure channel***

```

mailto:byrnejb.@harte-lyne.ca  
James B. Byrne Harte & Lyne Limited  
vox: +1 905 561 1241 9 Brockley Drive  
fax: +1 905 561 0757 Hamilton, Ontario  
 = hal Canada L8E 3C3

---

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 25, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327/2 "2006-01-25T20:07:57Z")

</div>

James B. Byrne wrote:

> If one considers only the issue of token parsing then surely the  
> construct \<%= if @items.size == 1 ? “true” : “false” %\> is  
> equivalent to \<%= if @items.size == 1 then “true” else “false” %\>.
> 
> It seems odd to me that the former works while the latter fails.  
> If there is a syntax reason why this behaviour is expected then I  
> would like to know what it is.

My guess would be, because Ruby needs to know where the line breaks are  
in a multi-line if-then-else statement. If you put is all on one line  
like:

if @items.size == 1 then “true” else “false”

You haven’t correctly told Ruby where the line breaks are located. You  
might be able to fake it with some semi-colons, but this, I believe, is  
why there is a one-line version of the if-then-else statement block…

-Brian

---

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 25, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327/3 "2006-01-25T20:17:06Z")

</div>

> If one considers only the issue of token parsing then surely the  
> construct \<%= if @items.size == 1 ? “true” : “false” %\> is  
> equivalent to \<%= if @items.size == 1 then “true” else “false” %\>.

I usually use the syntax \<%=(@items.size == 1 ? “true” : “false”) %\>

I’m not sure why this is, but in:

if @items.size == 1 ?

the “if” is not needed and may be just ignored with the presence of “?”.  
That would explain why one would work while the other doesn’t. I’m just  
trying to think from a parsing point of view and could be completely  
wrong.

---

<div class="post-metadata">

**Author:** ![James\_BSByrne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@James\_BSByrne](https://www.ruby-forum.com/u/James_BSByrne)\
**Post date:** [January 25, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/conditional-replacements-in-rhtml/97327/4 "2006-01-25T20:18:44Z")

</div>

I think what you’re after here is the pluralize helper.

> **[ActionView::Helpers::TextHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000420)**
>
> The TextHelper module provides a set of methods for filtering, formatting and transforming strings, which can reduce the amount of inline Ruby code in your views.

pluralize(@items.size, ‘item’) will result in “1 item”, or “2 items” etc  
…

To use the syntax you described you would need to put an ‘end’ at the  
end:

\<%= if @items.size == 1 then “true” else “false” end %\>

Cheers, -Jonny.

James B. Byrne wrote:

> On Wed, 25 Jan 2006 10:32:23 -0700, Ben R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > To do this with an if statement:
> > 
> > \<%if @items.size == 1 -%\>item\<% else -%\>items\<% end -%\>
> 
> Thanks you. Nonetheless I still remain uncertain why the following  
> construct does not work:
> 
> > Your cart currently holds \<%= @items.size %\> \<%= if @items.size == 1 then "item" else "items" %\>.
> 
> If one considers only the issue of token parsing then surely the  
> construct \<%= if @items.size == 1 ? “true” : “false” %\> is  
> equivalent to
> 
> It seems odd to me that the former works while the latter fails.  
> If there is a syntax reason why this behaviour is expected then I  
> would like to know what it is.
> 
> Regards,  
> Jim
