Forum: Ruby on Rails avoiding nil object errors with conditional checks?

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.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
How can I avoid nil object errors if a var isn't set?

<% if @var %><%=h @var %><% end %>

Also, is there a way to reduce the number of code
delimiters, something like:

<% if @var print @var %>

Thanks!
CSN



__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Ah!

<% if nil ^ @var %><%=h @var %><% end %>

I'm still interested in knowing if there's a way to
reduce the number of <% and %>'s!

Thanks,
CSN


--- CSN <cool_screen_name90001@yahoo.com> wrote:

> Thanks!
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>




__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-12 12:49
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 7, 2005, at 5:58 PM, CSN wrote:
>
> Ah!
>
> <% if nil ^ @var %><%=h @var %><% end %>
>
> I'm still interested in knowing if there's a way to
> reduce the number of <% and %>'s!

Since
   nil.to_s == ''
and
   h(nil) == ''
just do
   <%=h @var %>

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
uQTsZLuGgWoLIttk95/EPyk=
=KmyP
-----END PGP SIGNATURE-----
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Ah, easy enough. Is there a way to simplify this code?

<% if nil ^ @var %><%=h @var %>, <% end %><%=h @var2
%>

If @var isn't empty, I want a comma and space to
follow it. Otherwise just output @var2. Coming from
PHP's Smarty, I used:

{if $var}{$var}, {/if}{$var2}

Thanks
CSN


--- Jeremy Kemper <jeremy@bitsweat.net> wrote:

> to
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (Darwin)
>
>
iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
> uQTsZLuGgWoLIttk95/EPyk=
> =KmyP
> -----END PGP SIGNATURE-----
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>




__________________________________
Start your day with Yahoo! - Make it your home page!
http://www.yahoo.com/r/hs
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-12 12:49
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 7, 2005, at 6:18 PM, CSN wrote:
> Ah, easy enough. Is there a way to simplify this code?
>
> <% if nil ^ @var %><%=h @var %>, <% end %><%=h @var2
> %>
>
> If @var isn't empty, I want a comma and space to
> follow it. Otherwise just output @var2. Coming from
> PHP's Smarty, I used:
>
> {if $var}{$var}, {/if}{$var2}

Not sure it's simplified, but it's shorter:
   <%= "#{h(@var)}, " if @var %><%=h @var2 %>

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDcA3/AQHALep9HFYRAkCOAKDCzNcJ6UvmAhEoZXC3z9vyK9gmFwCeJ9lu
9ORPPphAaBNNt/TrnwfHBw4=
=K7r3
-----END PGP SIGNATURE-----
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
--- Jeremy Kemper <jeremy@bitsweat.net> wrote:

> to
> > reduce the number of <% and %>'s!
>
> Since
>    nil.to_s == ''
> and
>    h(nil) == ''
> just do
>    <%=h @var %>

That doesn't work for this though:

<%=h @var.name %>

It gives a nil object error.

CSN


>
> jeremy
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (Darwin)
>
>
iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
> uQTsZLuGgWoLIttk95/EPyk=
> =KmyP
> -----END PGP SIGNATURE-----
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>





__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
7ba164f40a50bc23dbb2aa825fb7bc16?d=identicon&s=25 h.wickham (Guest)
on 2005-11-12 12:49
(Received via mailing list)
> Not sure it's simplified, but it's shorter:
>    <%= "#{h(@var)}, " if @var %><%=h @var2 %>

Or maybe

<%=h [var, var2].compact.join(",") %>

especially useful if you have more than one variable.

Hadley
2585e5de3d03793e39077e3d8b73dfaf?d=identicon&s=25 ronnylist (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Mon, Nov 07, 2005 at 06:45:19PM -0800, CSN wrote:
> <%=h @var.name %>
>
> It gives a nil object error.

How about this?

<%= h(@var.name) if @var %>

Ronny
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-12 12:49
(Received via mailing list)
Nice. This does exactly what I want:

<%= h("#{@var.name}, ") if @var %>

Thanks everybody!
CSN


--- Ronny Haryanto <ronnylist@haryan.to> wrote:

> > _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>





__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
631950bd299098576e57053ce703e082?d=identicon&s=25 nshb (Guest)
on 2005-11-12 12:49
(Received via mailing list)
> > >
>
> That doesn't work for this though:
>
> <%=h @var.name %>
>
> It gives a nil object error.
>
> CSN
>

What I have used is

<%= h @var.name if !@var.nil? %>

Warmest regards,
Nathan.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-11-12 12:49
(Received via mailing list)
>What I have used is
>
><%= h @var.name if !@var.nil? %>
>
>
>
<%= h @var.name unless @var.nil? %>
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Nov 7, 2005, at 5:51 PM, CSN wrote:

> CSN
>
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Nov 7, 2005, at 5:51 PM, CSN wrote:

> CSN
Try this:

<%= @var rescue nil %>

HTH-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
6993d6b4b6d882f421e1b45ac147f3e0?d=identicon&s=25 scott (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Nov 7, 2005, at 11:21 PM, Ezra Zygmuntowicz wrote:

>> <% if @var print @var %>
>>
>> Thanks!
>> CSN
>
> Try this:
>
> <%= @var rescue nil %>

If @var has not been set, it will evaluate to nil, which when to_s'ed
(as I think Jeremy
pointed out) will be '', so this construct is not useful (as it will
not throw an exception
to be rescued).

Just doing

<%= @var %>

will show you what's in @var or '' if @var has been set (or is
otherwise nil).  To address
further up in the thread, you are not getting nil object errors from
this construct,
rather from something like <%= @foo.bar %>, and I think that has been
addressed
earlier with constructs like <%= @foo.bar if @foo %> and so on.
This topic is locked and can not be replied to.