Forum: Ruby on Rails conditional replacements in rhtml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James B. Byrne (Guest)
on 2006-01-25 19:58
(Received via mailing list)
On Wed, 25 Jan 2006 10:32:23 -0700, Ben Reubenstien <benr@x-cr.com>
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:

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

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.<token>@harte-lyne.ca
James B. Byrne                Harte & Lyne Limited
vox: +1 905 561 1241          9 Brockley Drive
fax: +1 905 561 0757          Hamilton, Ontario
<token> = hal                 Canada L8E 3C3

------- End of forwarded message -------
--
     *** e-mail is not a secure channel ***
mailto:byrnejb.<token>@harte-lyne.ca
James B. Byrne                Harte & Lyne Limited
vox: +1 905 561 1241          9 Brockley Drive
fax: +1 905 561 0757          Hamilton, Ontario
<token> = hal                 Canada L8E 3C3
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-01-25 20:07
(Received via mailing list)
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
18d8b0c948dd7b3d39c9667cfd4e817c?d=identicon&s=25 Andrew Stone (Guest)
on 2006-01-25 20:17
(Received via mailing list)
>
> 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.
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-25 20:18
I think what you're after here is the pluralize helper.
http://api.rubyonrails.org/classes/ActionView/Help...

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 Reubenstien <benr@x-cr.com>
> 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:
>
>> <p>
>>   Your cart currently holds <%= @items.size %>
>>   <%= if @items.size == 1 then "item" else "items" %>.
>> </p>
>
> 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
This topic is locked and can not be replied to.