# Combine @item.foo.nil? || @item.foo.empty??

**URL:** <https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074>\
**Category:** Ruby\
**Created:** [September 8, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074 "2006-09-08T22:50:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 8, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/1 "2006-09-08T22:50:17Z")

</div>

I have code like this in my (Markaby) templates:

tr do  
td @item.foo  
end unless (@item.foo.nil? || @item.foo.empty?)

Is there a way to combine these two OR clauses into one?

tr do  
td @item.foo  
end unless @item.foo.nil\_or\_empty?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 8, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/2 "2006-09-08T22:57:07Z")

</div>

> tr do  
> td @item.foo  
> end unless (@item.foo.nil? || @item.foo.empty?)
> 
> Is there a way to combine these two OR clauses into one?

Define a method on the class of foo?

class Foo  
def nil\_or\_empty?  
return nil? || empty?  
end  
end

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 9, 2006, 12:01am UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/3 "2006-09-09T00:01:14Z")

</div>

Matthew M. wrote:

> > tr do  
> > td @item.foo  
> > end unless (@item.foo.nil? || @item.foo.empty?)
> > 
> > Is there a way to combine these two OR clauses into one?
> 
> Define a method on the class of foo?
> 
> class Foo  
> ^^^  
> Object  
> def nil\_or\_empty?  
> return nil? || empty?  
> end  
> end

Otherwise it won’t be defined when foo==nil.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 9, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/4 "2006-09-09T16:40:28Z")

</div>

removed\_email\_address@domain.invalid writes:

> tr{ tr @item.foo } unless @item.foo.to\_s.empty?

It makes sense to me that

```
object.to_s.empty? # => true

```

should imply

```
object.empty? # => true

```

So why not just do

```
class NilClass
  def empty?; true; end
end

```

?

-Marshall

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 9, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/5 "2006-09-09T16:58:29Z")

</div>

On Sat, 9 Sep 2006, Marshall T. Vandegrift wrote:

> object.empty? # =\> true
> 
> So why not just do
> 
> class NilClass  
> def empty?; true; end  
> end
> 
> ?
> 
> -Marshall

def deep\_in\_some\_other\_unrelated\_code  
begin  
should\_always\_be\_empty = buggy\_method

```
   abort unless should_always_be_empty.empty? # OOPS!

   transfer_four_million_euros_to_swiss_account
 rescue
   hours_of_debugging?
 ensure
   modify_builtins_with_care!
 end

```

end

-a

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 9, 2006, 12:13am UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/6 "2006-09-09T00:13:16Z")

</div>

On Sat, 9 Sep 2006, Joe R. wrote:

> end unless @item.foo.nil\_or\_empty?
> 
> Thanks,  
> Joe

tr{ tr @item.foo } unless @item.foo.to\_s.empty?

-a

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 9, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/7 "2006-09-09T22:23:15Z")

</div>

On Sep 9, 2006, at 10:37 AM, Marshall T. Vandegrift wrote:

> ```
> object.empty? # => true
> 
> ```

Doesn’t to me. nil is not a container, and it should not be treated  
as one. A string is a container (contains a sequence of characters)  
and can therefore be empty. The fact that one, arbitrary string  
representation of nil happens to be the empty string doesn’t mean  
that nil is empty. (This is also why I disklike rails’s #blank?, but  
at least it’s named something different.)

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 10, 2006, 4:46am UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/8 "2006-09-10T04:46:04Z")

</div>

On Sat, 09 Sep 2006 06:53:59 +0900, Joel VanderWerf wrote:

> > class Foo  
> > ^^^  
> > Object  
> > def nil\_or\_empty?  
> > return nil? || empty?  
> > end  
> > end
> 
> Otherwise it won’t be defined when foo==nil.

So define as follows:

class NilClass  
def nil\_or\_empty?  
true  
end  
end

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 10, 2006, 10:12am UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/9 "2006-09-10T10:12:22Z")

</div>

On 08/09/06, Joe R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end unless @item.foo.nil\_or\_empty?
> 
> Thanks,  
> Joe
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

tr do  
td @item.foo  
end unless Array(@item).empty?

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 10, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/10 "2006-09-10T16:00:48Z")

</div>

On 9/8/06, Joe R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> td @item.foo  
> end unless @item.foo.nil\_or\_empty?
> 
> Thanks,  
> Joe

Perhaps #blank? from activesupport [1] could do what you want?

-Scott

[1]  
[http://dev.rubyonrails.org/browser/tags/rel\_1-1-6/activesupport/lib/active\_support/core\_ext/blank.rb](http://dev.rubyonrails.org/browser/tags/rel_1-1-6/activesupport/lib/active_support/core_ext/blank.rb)

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 11, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/combine-item-foo-nil-item-foo-empty/68074/11 "2006-09-11T23:59:18Z")

</div>

On 9/9/06, Marshall T. Vandegrift [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> class NilClass
> def empty?; true; end
> end
> 
> ```

Because nil isn’t empty.

It isn’t a container. It’s not an array, hash, or string.

Rails has a #blank? method – which I’ve used this weekend – but  
nil.empty? is a false question.

-austin
