# Need help with a simple IF/ELSE statement?

**URL:** <https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209>\
**Category:** Rails\
**Created:** [October 15, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209 "2006-10-15T17:48:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [October 15, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209/1 "2006-10-15T17:48:26Z")

</div>

I’m using hivelogic’s email enkoder and need help with a simple if/else  
statement. If there is a value for participant.student.email I would  
like to create the hyperlink for the email address, if not I would just  
like to list their name with no link. Can someone tell me how to do  
this? I can’t seem to get it to work or find any good information on  
using nil with IF/ELSE statements.

[code]@name = “#{participant.student.last\_name},  
#{participant.student.first\_name}”  
@email = participant.student.email

if !@email.nil?  
enkode\_mail(@email, @name)  
else  
@name  
end[/code]

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [October 15, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209/2 "2006-10-15T18:10:39Z")

</div>

Looks OK to me, but I don’t know how enkode\_mail works.

Maybe you meant to assign the result of enkode\_mail(…) to @email?

@email = enkode\_mail(@email, @name)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [October 15, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209/3 "2006-10-15T18:34:43Z")

</div>

On Sun, Oct 15, 2006, John S. wrote:

> I’m using hivelogic’s email enkoder and need help with a simple if/else  
> statement. If there is a value for participant.student.email I would  
> like to create the hyperlink for the email address, if not I would just  
> like to list their name with no link. Can someone tell me how to do  
> this? I can’t seem to get it to work or find any good information on  
> using nil with IF/ELSE statements.

Try using #blank? instead of #nil?. In all likelihood, what’s happening  
is you’re getting an empty string back from the database when it’s not  
present.

Also, you might consider using unless instead of if !:

unless @email.blank?  
enkode\_mail @email, @name  
else  
@name  
end

Ben

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [October 15, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209/4 "2006-10-15T18:47:50Z")

</div>

Thanks for the help!!

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [October 15, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/need-help-with-a-simple-if-else-statement/72209/5 "2006-10-15T19:16:51Z")

</div>

Hi –

On Sun, 15 Oct 2006, Ben B. wrote:

> Try using #blank? instead of #nil?. In all likelihood, what’s happening  
> is you’re getting an empty string back from the database when it’s not  
> present.
> 
> Also, you might consider using unless instead of if !:
> 
> unless @email.blank?  
> enkode\_mail @email, @name  
> else  
> @name  
> end

Or, if like me you prefer to avoid the somewhat awkward unless/else  
sequence, you can switch the whole thing around:

if @email.blank?  
@name

etc.

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)
