In a rails app I have a sprintf("#0.2f",x.price) formatted value that may be nil. What is the preferred (short-elegant-terse) ruby way of testing for this and displaying a substitute text ("n/a") in place of the missing numeric value? 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
x.price && sprintf("%.2f", x.price) || ''n/a'' 2006/1/27, James B. Byrne <ByrneJB@harte-lyne.ca>:> In a rails app I have a sprintf("#0.2f",x.price) formatted value > that may be nil. What is the preferred (short-elegant-terse) ruby > way of testing for this and displaying a substitute text ("n/a") in > place of the missing numeric value? > > Regards, > Jim >
On 27 Jan 2006 at 10:30, rails@lists.rubyonrails.org wrote:> In a rails app I have a sprintf("#0.2f",x.price) formatted value > that may be nil. What is the preferred (short-elegant-terse) > ruby way of testing for this and displaying a substitute text > ("n/a") in place of the missing numeric value?I did this and it works. <%= product.price ? sprintf("%0.2f", product.price) : "n/a" %> -- *** 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