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
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-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> 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 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
Jeremy Kemper
2005-Nov-08 02:03 UTC
Re: avoiding nil object errors with conditional checks?
-----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-----
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-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> -----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----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Start your day with Yahoo! - Make it your home page! http://www.yahoo.com/r/hs
Jeremy Kemper
2005-Nov-08 02:31 UTC
Re: avoiding nil object errors with conditional checks?
-----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-----
--- Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> -----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 %>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-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
hadley wickham
2005-Nov-08 02:49 UTC
Re: avoiding nil object errors with conditional checks?
> 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
Ronny Haryanto
2005-Nov-08 03:00 UTC
Re: avoiding nil object errors with conditional checks?
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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Nice. This does exactly what I want: <%= h("#{@var.name}, ") if @var %> Thanks everybody! CSN --- Ronny Haryanto <ronnylist-fjQGkq+J5uxhl2p70BpVqQ@public.gmane.org> wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
Nathaniel S. H. Brown
2005-Nov-08 03:26 UTC
RE: avoiding nil object errors with conditional checks?
> --- Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote: > > > -----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 %> > > 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. -------------------------------------------------------------- Nathaniel S. H. Brown Toll Free 1.877.4.INIMIT Inimit Innovations Phone 604.724.6624 www.inimit.com Fax 604.444.9942
>What I have used is > ><%= h @var.name if !@var.nil? %> > > ><%= h @var.name unless @var.nil? %>
Ezra Zygmuntowicz
2005-Nov-08 04:21 UTC
Re: avoiding nil object errors with conditional checks?
On Nov 7, 2005, at 5:51 PM, CSN wrote:> 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 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Ezra Zygmuntowicz
2005-Nov-08 04:21 UTC
Re: avoiding nil object errors with conditional checks?
On Nov 7, 2005, at 5:51 PM, CSN wrote:> 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! > CSNTry this: <%= @var rescue nil %> HTH- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Scott Barron
2005-Nov-08 13:46 UTC
Re: avoiding nil object errors with conditional checks?
On Nov 7, 2005, at 11:21 PM, Ezra Zygmuntowicz wrote:> > On Nov 7, 2005, at 5:51 PM, CSN wrote: > >> 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 > > 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. -- Scott Barron Lunchbox Software http://lunchboxsoftware.com http://lunchroom.lunchboxsoftware.com http://rubyi.st