# ORing of strings

**URL:** <https://www.ruby-forum.com/t/oring-of-strings/114033>\
**Category:** Rails\
**Created:** [September 7, 2007, 7:49am UTC](https://www.ruby-forum.com/t/oring-of-strings/114033 "2007-09-07T07:49:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [September 7, 2007, 7:49am UTC](https://www.ruby-forum.com/t/oring-of-strings/114033/1 "2007-09-07T07:49:01Z")

</div>

Hi,

I wanted to do this: get display\_name as one of those nick\_name, first  
and last in that order.  
@display\_name = @nick\_name || @first\_name || @second\_name

but if nick\_name turns out be “” and not ni. so @display\_name gets “”  
and not the values in first or second.

How do we handle this situation?

Regards,  
Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [September 7, 2007, 8:01am UTC](https://www.ruby-forum.com/t/oring-of-strings/114033/2 "2007-09-07T08:01:04Z")

</div>

Sandeep G. wrote:

> Hi,
> 
> I wanted to do this: get display\_name as one of those nick\_name, first  
> and last in that order.  
> @display\_name = @nick\_name || @first\_name || @second\_name
> 
> but if nick\_name turns out be “” and not ni. so @display\_name gets “”  
> and not the values in first or second.
> 
> How do we handle this situation?
> 
> Regards,  
> Sandeep G

I dont really understand what you mean but why not put them in array 🙂

@display\_name = [“nick\_name”, “asd”, “asd”]

and then

@display\_name.each do |val|  
if nil or if “” then do that or this?  
end

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [September 7, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/oring-of-strings/114033/3 "2007-09-07T20:46:34Z")

</div>

On Sep 7, 2007, at 1:49 AM, Sandeep G. wrote:

> How do we handle this situation?
> 
> Regards,  
> Sandeep G

I handle this by creating a String#nonblank? to work like  
Numeric#nonzero? (see docs)

class String

# Allowing a chain like: string\_value.nonblank? || ‘default value’

def nonblank?  
self unless blank?  
end  
end

and to duck-type when nil:

class NilClass

# Allowing a chain like: value.nonblank? || ‘default value’

def nonblank?  
self  
end

# so it plays nicely with Numeric#nonzero?

def nonzero?  
self  
end  
end

Then your example becomes:

@nick\_name = nil  
@first\_name = “”  
@second\_name = “Sandeep”

> > @display\_name = @nick\_name.nonblank? || @first\_name.nonblank? ||  
> > @second\_name.nonblank?  
> > =\> “Sandeep”

You can actually leave the .nonblank? off of the last part if you  
don’t care nil v. ‘’ depending on @second\_name.

-Rob

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