Avoiding nil object errors with conditional checks?


#1

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.


#2

-----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-----


#3

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 removed_email_address@domain.invalid wrote:

Thanks!
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! FareChase: Search multiple travel sites in one click.


#4

-----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-----


#5

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 K. removed_email_address@domain.invalid wrote:

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

iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG

uQTsZLuGgWoLIttk95/EPyk=
=KmyP
-----END PGP SIGNATURE-----


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Start your day with Yahoo! - Make it your home page!


#6

— Jeremy K. removed_email_address@domain.invalid 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! Mail - PC Magazine Editors’ Choice 2005
http://mail.yahoo.com


#7

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


#8

Nice. This does exactly what I want:

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

Thanks everybody!
CSN

— Ronny H. removed_email_address@domain.invalid wrote:


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! Mail - PC Magazine Editors’ Choice 2005
http://mail.yahoo.com


#9

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.


#10

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


#11

What I have used is

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

<%= h @var.name unless @var.nil? %>


#12

On Nov 7, 2005, at 5:51 PM, CSN wrote:

CSN
Try this:

<%= @var rescue nil %>

HTH-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#13

On Nov 7, 2005, at 5:51 PM, CSN wrote:

CSN

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#14

On Nov 7, 2005, at 11:21 PM, Ezra Z. 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.