# Checking for nil

**URL:** <https://www.ruby-forum.com/t/checking-for-nil/183132>\
**Category:** Rails\
**Created:** [February 8, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132 "2010-02-08T19:53:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/1 "2010-02-08T19:53:10Z")

</div>

HI, I have inherited support on a rails website and I am encountering  
the following problem:

\<% if @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’)!=nil %\>  
\<%= stylesheet\_link\_tag ‘ie6’ %\>  
\<% elsif  
@request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
\<%= stylesheet\_link\_tag ‘ie7’ %\>  
\<% else %\>  
\<%= stylesheet\_link\_tag ‘moz’ %\>  
\<% end %\>  
this little code snippet works except when the user agent string is  
empty in which case i get : (undefined method `downcase’ for  
nil:NilClass)

how can i edit this code so that it can handle an empty user agent  
string?

thanks

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/2 "2010-02-08T20:11:23Z")

</div>

Ketema H. wrote:

> HI, I have inherited support on a rails website and I am encountering  
> the following problem:
> 
> \<% if @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’)!=nil %\>  
> \<%= stylesheet\_link\_tag ‘ie6’ %\>  
> \<% elsif  
> @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
> \<%= stylesheet\_link\_tag ‘ie7’ %\>  
> \<% else %\>  
> \<%= stylesheet\_link\_tag ‘moz’ %\>  
> \<% end %\>  
> this little code snippet works except when the user agent string is  
> empty in which case i get : (undefined method `downcase’ for  
> nil:NilClass)
> 
> how can i edit this code so that it can handle an empty user agent  
> string?
> 
> thanks

Introduce a call to nil? . No problem.

And for goodness’ sakes, get this code out of the view file. Logic  
doesn’t belong there.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 8:13pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/3 "2010-02-08T20:13:37Z")

</div>

Hi Katerna,

Rails has a handy helper for this. blank? returns true for false, empty  
or whitespace strings. nil is false. You’ll need to move the negation  
to the beginning.

On Mon, 2010-02-08 at 19:53 +0100, Ketema H. wrote:

> % if @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’)!=nil

if !@request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’).blank?

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 8:13pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/4 "2010-02-08T20:13:52Z")

</div>

Ketema H. wrote:

> HI, I have inherited support on a rails website and I am encountering  
> the following problem:
> 
> \<% if @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’)!=nil %\>  
> \<%= stylesheet\_link\_tag ‘ie6’ %\>  
> If the user agent is for IE6…

> ```
> <% elsif
> 
> ```
> 
> @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
> \<%= stylesheet\_link\_tag ‘ie7’ %\>  
> If the user agent is for IE7…

> ```
> <% else %>
> <%= stylesheet_link_tag 'moz' %>
> 
> ```

If the user agent is anything else, including empty …

> ```
> <% end %>
> 
> ```
> 
> this little code snippet works except when the user agent string is  
> empty in which case i get : (undefined method `downcase’ for  
> nil:NilClass)
> 
> how can i edit this code so that it can handle an empty user agent  
> string?

Ah, I see. You want ‘nil’ … So you should first say that :  
if @request.env[‘HTTP\_USER\_AGENT’].nil?  
… And then whatever you want to do.

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/5 "2010-02-08T20:54:36Z")

</div>

On Feb 8, 2010, at 2:13 PM, Aldric G. wrote:

> > @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
> > nil:NilClass)
> > 
> > how can i edit this code so that it can handle an empty user agent  
> > string?
> 
> Ah, I see. You want ‘nil’ … So you should first say that :  
> if @request.env[‘HTTP\_USER\_AGENT’].nil?  
> … And then whatever you want to do.

Start with something like:

user\_agent = @request.env.fetch(‘HTTP\_USER\_AGENT’, ‘empty’).downcase

Then replace all the other tests:

if user\_agent.index(‘msie 6.0’)

There’s no need to say !=nil

(but I agree that you should get the logic out of the view if  
possible. Perhaps in a helper?

def user\_agent  
@request.env.fetch(‘HTTP\_USER\_AGENT’, ‘empty’).downcase  
end

def browser\_specific\_stylesheet  
case user\_agent  
when /msie 6.0/  
‘ie6’  
when /msie 7.0/  
‘ie7’  
else  
‘moz’  
end  
end

\<%= stylesheet\_tag browser\_specific\_stylesheet %\>

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/6 "2010-02-08T21:19:44Z")

</div>

Rob B. wrote:

> On Feb 8, 2010, at 2:13 PM, Aldric G. wrote:
> 
> > > @request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
> > > nil:NilClass)
> > > 
> > > how can i edit this code so that it can handle an empty user agent  
> > > string?
> > 
> > Ah, I see. You want ‘nil’ … So you should first say that :  
> > if @request.env[‘HTTP\_USER\_AGENT’].nil?  
> > … And then whatever you want to do.
> 
> Start with something like:
> 
> user\_agent = @request.env.fetch(‘HTTP\_USER\_AGENT’, ‘empty’).downcase
> 
> Then replace all the other tests:
> 
> if user\_agent.index(‘msie 6.0’)
> 
> There’s no need to say !=nil
> 
> (but I agree that you should get the logic out of the view if  
> possible. Perhaps in a helper?
> 
> def user\_agent  
> @request.env.fetch(‘HTTP\_USER\_AGENT’, ‘empty’).downcase  
> end
> 
> def browser\_specific\_stylesheet  
> case user\_agent  
> when /msie 6.0/  
> ‘ie6’  
> when /msie 7.0/  
> ‘ie7’  
> else  
> ‘moz’  
> end  
> end
> 
> \<%= stylesheet\_tag browser\_specific\_stylesheet %\>
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

thanks for all the replies! i got a check to work for all cases:

\<% if !@request.env[‘HTTP\_USER\_AGENT’].nil?  
if  
@request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 6.0’)!=nil %\>  
\<%= stylesheet\_link\_tag ‘ie6’ %\>  
\<% elsif  
@request.env[‘HTTP\_USER\_AGENT’].downcase.index(‘msie 7.0’)!=nil %\>  
\<%= stylesheet\_link\_tag ‘ie7’ %\>  
\<% else %\>  
\<%= stylesheet\_link\_tag ‘moz’ %\>  
\<% end %\>  
\<% end %\>  
not the prettiest code, but its working and no more error

thanks again

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 9, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/7 "2010-02-09T17:08:41Z")

</div>

On Feb 8, 2:52 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (but I agree that you should get the logic out of the view if  
> possible. Perhaps in a helper?

He should get the logic out of the app _entirely_. This is the exact  
use case for IE’s conditional comments:

> **[About conditional comments (Internet Explorer)](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)?redirectedfrom=MSDN)**

–Matt J.

---

<div class="post-metadata">

**Author:** ![ketema](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6bbea6/32.png) [@ketema](https://www.ruby-forum.com/u/ketema)\
**Post date:** [February 8, 2010, 11:10pm UTC](https://www.ruby-forum.com/t/checking-for-nil/183132/8 "2010-02-08T23:10:27Z")

</div>

On Mon, Feb 8, 2010 at 12:53 PM, Ketema H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> <% end %>
> 
> ```
> 
> this little code snippet works except when the user agent string is  
> empty in which case i get : (undefined method `downcase’ for  
> nil:NilClass)
> 
> how can i edit this code so that it can handle an empty user agent  
> string?

@agent = @request.env[‘HTTP\_USER\_AGENT’] rescue nil

\<% if @agent -%\>  
…  
\<% end -%\>

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)
