# If the variable is set

**URL:** <https://www.ruby-forum.com/t/if-the-variable-is-set/196398>\
**Category:** Ruby\
**Created:** [October 12, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398 "2010-10-12T18:57:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/1 "2010-10-12T18:57:05Z")

</div>

How can I create an if statement to check if a variable contains a  
value? If it does contain a value, I want to send an email. I’m doing  
the following but it doesn’t work…

* * *

## EXTRACT

def create  
@appointment = Appointment.new(params[:appointment])

```
respond_to do |format|
  if @appointment.save

    # send email only if a client email is set
    if defined?(appointment.client.email)
      Notifier.appointment_booked(@appointment).deliver
    end

```

* * *

* * *

## FULL CONTROLLER

class AppointmentsController \< ApplicationController  
before\_filter :load\_clients\_and\_services, :only =\> [ :new, :create,  
:edit ]

# GET /appointments

# GET /appointments.xml

def index  
@appointments = Appointment.all(:order =\> ‘start’, :conditions =\> [  
“start \>= ?”, Date.today ] )  
appointments = Appointment.all(:order =\> ‘start’, :conditions =\> [  
“start \>= ?”, Date.today ] )  
appointments.group\_by do |appointment|  
appointment.start.strftime("%Y%m%d")  
end

```
@past_appointments = Appointment.all(:order => 'start', :conditions

```

=\> [“start \< ?”, Date.today] )

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @appointments }
end

```

end

# GET /appointments/1

# GET /appointments/1.xml

def show  
@appointment = Appointment.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @appointment }
end

```

end

# GET /appointments/new

# GET /appointments/new.xml

def new  
@appointment = Appointment.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @appointment }
end

```

end

# GET /appointments/1/edit

def edit  
@appointment = Appointment.find(params[:id])  
end

# POST /appointments

# POST /appointments.xml

def create  
@appointment = Appointment.new(params[:appointment])

```
respond_to do |format|
  if @appointment.save

    # send email only if a client email is set
    if defined?(appointment.client.email)
      Notifier.appointment_booked(@appointment).deliver
    end

    format.html { redirect_to(@appointment, :notice => 'Appointment

```

was successfully created.’) }  
format.xml { render :xml =\> @appointment, :status =\> :created,  
:location =\> @appointment }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @appointment.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

# PUT /appointments/1

# PUT /appointments/1.xml

def update  
@appointment = Appointment.find(params[:id])

```
respond_to do |format|
  if @appointment.update_attributes(params[:appointment])
    format.html { redirect_to(@appointment, :notice => 'Appointment

```

was successfully updated.’) }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @appointment.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

# DELETE /appointments/1

# DELETE /appointments/1.xml

def destroy  
@appointment = Appointment.find(params[:id])  
@appointment.destroy

```
respond_to do |format|
  format.html { redirect_to(appointments_url) }
  format.xml { head :ok }
end

```

end

private  
def load\_clients\_and\_services  
@clients = Client.find(:all)  
@services = Service.find(:all)  
end

end

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/2 "2010-10-12T19:20:11Z")

</div>

On Tue, Oct 12, 2010 at 11:57 AM, Leonel _._  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ```
> end
> 
> ```
> 
> # GET /appointments
> 
> ## @past\_appointments = Appointment.all(:order =\> ‘start’, :conditions def show def new @appointment = Appointment.find(params[:id]) # send email only if a client email is set format.xml { render :xml =\> @appointment.errors, :status =\> respond\_to do |format| end end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi, Leonel, this forum is for Ruby, the programming language. It looks  
like  
your question is about Rails, the web framework. Since you posted from  
ruby-forum, you should be able to the ruby on rails forum by selecting  
the  
next forum down on the main list. You’ll want to repost your question  
there.

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/3 "2010-10-12T19:32:42Z")

</div>

Looks like you’re checking whether or not the variable itself exists, as  
opposed to if it contains a value.

Try this instead:

if appointment.client.email  
#do stuff & things  
end

In this instance, if ‘email == nil’, the statement will evaluate to  
false.

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/4 "2010-10-12T19:50:42Z")

</div>

@Josh: I’m sorry I thought since all I need to create is a Ruby if  
statement it would fall under the Ruby forum. It’s true I’m using it in  
a Rails context but it basically is just a Ruby question. But if you  
still want me to move it I will.

@Alex: could you please help me in the Ruby on Rails forum?

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/5 "2010-10-12T20:09:43Z")

</div>

On Tue, Oct 12, 2010 at 1:50 PM, Leonel _._ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @Josh: I’m sorry I thought since all I need to create is a Ruby if  
> statement it would fall under the Ruby forum. It’s true I’m using it in  
> a Rails context but it basically is just a Ruby question. But if you  
> still want me to move it I will.
> 
> @Alex: could you please help me in the Ruby on Rails forum?

No, this is clearly a Ruby question, albeit in the context of Rails.  
Some folks here are just a llttle too eager to pull there “this is the  
Ruby not the Rails” forum trigger.

a = []  
defined? a # =\> “local-variable”  
defined? a.last # =\> “method”  
defined? a.last.foo # =\> nil  
a.last.nil? # =\> true  
defined? a.last.nil? # =\> “method”

So  
if defined?(appointment.client.email)

returns “method” if appointment refers to an object with a client  
method which returns an object with an email method.

It doesn’t send that last message.

Normally in Ruby if you would simply check that the email value was  
non-nil and idiomatically this would just be

if appointment.client.email  
#…  
end

Because nil and false are the only non-truthy values in Ruby and it  
would be unusual to set the email to false.

–  
Rick DeNatale

Help fund my talk at Ruby Conf 2010:[http://pledgie.com/campaigns/13677](http://pledgie.com/campaigns/13677)  
Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![leonel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4491bb/32.png) [@leonel](https://www.ruby-forum.com/u/leonel)\
**Post date:** [October 12, 2010, 8:25pm UTC](https://www.ruby-forum.com/t/if-the-variable-is-set/196398/6 "2010-10-12T20:25:37Z")

</div>

I was supposed to check for empty? because it’s an empty string.

The answer is in the moved post: [If variable is not empty - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/221968)

Thanks Rick, that list will also help me in the future 😃
