Forum: Ruby on Rails Ruby sprintf style question

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.
James B. Byrne (Guest)
on 2006-01-27 17:32
(Received via mailing list)
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
Lugovoi N. (Guest)
on 2006-01-27 17:38
(Received via mailing list)
x.price && sprintf("%.2f", x.price) || 'n/a'

2006/1/27, James B. Byrne <removed_email_address@domain.invalid>:
This topic is locked and can not be replied to.