# Re: testing for nil or empty and then

**URL:** <https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687>\
**Category:** Rails\
**Created:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687 "2006-10-18T10:40:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/1 "2006-10-18T10:40:47Z")

</div>

Try

If !params[:category\_id] || params[:category\_id].empty?

If the first part of the conditional is true, Ruby will ignore the next  
for  
speed. (Careful of this if you use a function as a conditional and rely  
on  
it being run regardless of the outcome)

You could probably do:

If(params[:category\_id] || “”).empty

If params[:category\_id] is nil, the evaluation of the parens will be  
“”.

Ben

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/2 "2006-10-18T10:40:47Z")

</div>

blank? is useful for all-purpose emptiness testing

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/3 "2006-10-18T10:40:48Z")

</div>

No, that’ll blow up according to my irb session.

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/4 "2006-10-18T10:40:50Z")

</div>

Hi –

On Fri, 13 Oct 2006, Ben wrote:

> No, that’ll blow up according to my irb session.

If you’re talking about blank?, it’s defined in Rails but not in Ruby,  
so in a normal irb session it won’t be available.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/5 "2006-10-18T10:40:52Z")

</div>

On 10/13/06, Ben D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> for speed. (Careful of this if you use a function as a conditional and rely
> 
> If params[:category\_id] is nil, the evaluation of the parens will be “”.
> 
> Ben

Not sure where to go at this point.  
Trying the above with no success.  
I had also tried blank and didn’t help.  
Right now, using CriteriaQuery plugin my statements look like this (i  
removed the if statement as it was giving me a nil error):

pq = Position.query  
pq.category\_id\_in(params[:category\_id])  
pq.state\_id\_in(params[:state\_id])  
pq.term\_id\_in(params[:term\_id])  
pq.city\_in(params[:city].split(‘,’))  
pq.title\_in(params[:title].split(‘,’))  
pp pq  
@positions = pq.find

Which seems to work on their own (without and if clause)  
Here I only chose one field (printout using PP):

#\<Criteria::Query:0xab214e8  
@join\_aliases={“positions”=\>“positions”},  
@model\_class=Position,  
@restrictions=  
[#\<Criteria::In:0xab20cd8  
@attribute\_name=“category\_id”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xab214e8 …\>,  
@query=#\<Criteria::Query:0xab214e8 …\>,  
@restrictions=[],  
@value=“3”\>,  
#\<Criteria::In:0xab20b88  
@attribute\_name=“state\_id”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xab214e8 …\>,  
@query=#\<Criteria::Query:0xab214e8 …\>,  
@restrictions=[],  
@value=nil\>,  
#\<Criteria::In:0xab20a68  
@attribute\_name=“term\_id”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xab214e8 …\>,  
@query=#\<Criteria::Query:0xab214e8 …\>,  
@restrictions=[],  
@value=nil\>,  
#\<Criteria::In:0xab208b8  
@attribute\_name=“city”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xab214e8 …\>,  
@query=#\<Criteria::Query:0xab214e8 …\>,  
@restrictions=[],  
@value=[]\>,  
#\<Criteria::In:0xab20720  
@attribute\_name=“title”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xab214e8 …\>,  
@query=#\<Criteria::Query:0xab214e8 …\>,  
@restrictions=[],  
@value=[]\>]\>

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/6 "2006-10-18T10:44:01Z")

</div>

On 10/13/06, Ben D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> for speed. (Careful of this if you use a function as a conditional and rely
> 
> If params[:category\_id] is nil, the evaluation of the parens will be “”.
> 
> Ben

Since these did not work I am trying to alter them to better suit the  
call.  
Trying  
unless(params[:category\_id]).empty?  
pq.category\_id\_in(params[:category\_id])

ArgumentError in AjaxsearchController#list

wrong number of arguments (1 for 0)

Not sure why the unless part is illegal, or is maybe it’s the assignment  
after.  
Perhaps there is a better way to phrase it. My thinking is to check if  
a  
value exists first then make the assignment.

Stuart

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/7 "2006-10-18T10:40:52Z")

</div>

Fantastic!

I’ll use a console next time ;^)

---

<div class="post-metadata">

**Author:** ![badelatng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@badelatng](https://www.ruby-forum.com/u/badelatng)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/re-testing-for-nil-or-empty-and-then/72687/8 "2006-10-18T10:44:02Z")

</div>

Hey guys , woo woo ! I finally got it right.  
pq.category\_id\_in(params[:category\_id])unless(params[:category\_id]).empty?

Unless …yep!

Thanks for the help  
Stuart

On 10/13/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > If(params[:category\_id] || “”).empty
> 
> Not sure why the unless part is illegal, or is maybe it’s the assignment  
> after.  
> Perhaps there is a better way to phrase it. My thinking is to check if a  
> value exists first then make the assignment.
> 
> Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...
