Forum: Ruby on Rails shortcut for ternary operation in views

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
19062fbe329ec034ef54729fa9789e8d?d=identicon&s=25 Mark Veerman (Guest)
on 2007-02-08 15:21
(Received via mailing list)

I attended a recent Rails Edge conference and seem to recall a new
shortcut to the following, frequently used, code:
<%= foo.nil? ? '' : foo %>

Does anyone know what that might be?

Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-02-08 16:38
(Received via mailing list)
On Feb 8, 2007, at 9:20 AM, Mark Veerman wrote:
> Mark
Well, it's nothing new:

<%= "#{foo}" %>

Since nil.to_s is an empty string and string interpolation does a .to_s

<%= foo.to_s %>

also works.


Rob Biedenharn
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-02-08 16:58
(Received via mailing list)
Hi --

On Thu, 8 Feb 2007, Rob Biedenharn wrote:

> also works.

<%= %> does a to_s too, so:

   <%= foo %>

should be OK.


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (
    (See what readers are saying!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (
This topic is locked and can not be replied to.