I am working on the Agile programming tutorial and trying out some ruby code. Can someone tell me why the following works: <h1>Display Cart</h1> <p> Your cart currently holds <%= @items.size %> <%= @items.size == 1 ? "item" : "items" %>. </p> while this does not? <h1>Display Cart</h1> <p> Your cart currently holds <%= @items.size %> <%= if @items.size == 1 then "item" else "items" %>. </p> -- *** 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
Hi James, To do this with an if statement: <%if @items.size == 1 -%>item<% else -%>items<% end -%> I like this better myself. <%= @items.size == 1 ? "item" : "items" %> ~ Ben On 1/25/06, James B. Byrne <ByrneJB@harte-lyne.ca> wrote:> > I am working on the Agile programming tutorial and trying out some > ruby code. Can someone tell me why the following works: > > <h1>Display Cart</h1> > > <p> > Your cart currently holds <%= @items.size %> > <%= @items.size == 1 ? "item" : "items" %>. > </p> > > while this does not? > > <h1>Display Cart</h1> > > <p> > Your cart currently holds <%= @items.size %> > <%= if @items.size == 1 then "item" else "items" %>. > </p> > > > -- > *** 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060125/27a98d1a/attachment.html
Ben Reubenstien wrote:> Hi James, > > To do this with an if statement: > > <%if @items.size == 1 -%>item<% else -%>items<% end -%> > > I like this better myself. > > <%= @items.size == 1 ? "item" : "items" %> > > ~ BenThis is probably more railsy. <p> Your cart currently holds <%= pluralize(@items.size,"item") %>. </p> -- Posted via http://www.ruby-forum.com/.
you forgot the end after else "items" James B. Byrne wrote:>I am working on the Agile programming tutorial and trying out some >ruby code. Can someone tell me why the following works: > ><h1>Display Cart</h1> > ><p> > Your cart currently holds <%= @items.size %> > <%= @items.size == 1 ? "item" : "items" %>. ></p> > >while this does not? > ><h1>Display Cart</h1> > ><p> > Your cart currently holds <%= @items.size %> > <%= if @items.size == 1 then "item" else "items" %>. ></p> > > >-- > *** 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 > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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
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
> > 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. -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060125/02648c13/attachment.html
I think what you''re after here is the pluralize helper. http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000420 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-- Posted via http://www.ruby-forum.com/.
On 25 Jan 2006 at 19:20, Jonathan Viney <jviney@spreydon.org.nz> wrote:> 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 %>Yes, that works and answers my original question.> I think what you''re after here is the pluralize helper. > http://api.rubyonrails.org/classes/ActionView/Helpers/ > TextHelper.html#M000420 > > pluralize(@items.size, ''item'') will result in "1 item", > or "2 items" etc. ... >... but this really solves the problem. Many thanks! 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