# If defined on var

**URL:** <https://www.ruby-forum.com/t/if-defined-on-var/173990>\
**Category:** Ruby\
**Created:** [August 19, 2009, 4:22am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990 "2009-08-19T04:22:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 19, 2009, 4:22am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/1 "2009-08-19T04:22:15Z")

</div>

db =  
Amalgalite::Database.new("/usr/local/vrep/repo/db/development.sqlite3")  
table\_exists = db.execute(“select name from sqlite\_master where  
type=‘table’ and tbl\_name=‘derek’”)

if defined?(table\_exists)  
p “yes”  
end  
exit

Why does this print “yes”? There is no table called derek in my DB. In  
fact it prints yes no matter what I type in for tbl\_name.

My goal is to see if a table exists by testing the var holding the data  
is true/defined or not. Will you help?

thank you  
derek!

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 19, 2009, 4:34am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/2 "2009-08-19T04:34:38Z")

</div>

Am Mittwoch 19 August 2009 04:22:18 schrieb Derek S.:

> My goal is to see if a table exists by testing the var holding the data  
> is true/defined or not. Will you help?

true and defined are two very different things. Take this snippet as an  
example:

x = 42  
y = false  
z = nil

Here the variables x, y and z are defined. The variables a, b and  
foobarbaz  
are not. In short: once you assign a value to a variable that variable  
is  
defined - no matter whether that value was true or not. If you want to  
check  
whether the value a variable holds is true or not, do that. Don’t use  
defined?.  
In your case you can just do if table\_exists which even reads more  
naturally.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 19, 2009, 6:07am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/3 "2009-08-19T06:07:07Z")

</div>

Hi,  
Am Mittwoch, 19. Aug 2009, 11:22:18 +0900 schrieb Derek S.:

> table\_exists = db.execute( …)
> 
> if defined?(table\_exists)  
> p “yes”  
> end  
> exit
> 
> Why does this print “yes”?

irb(main):001:0\> x = nil  
=\> nil  
irb(main):002:0\> defined? x  
=\> “local-variable”  
irb(main):003:0\> defined? y  
=\> “method”  
irb(main):004:0\> defined? z  
=\> nil  
^^^  
^^^^^  
irb(main):005:0\> y  
ArgumentError: wrong number of arguments (0 for 1)  
from (irb):5:in `y’  
from (irb):5  
irb(main):006:0\>

Ugh, I did not know that `y’ is a method.

Be aware that `defined?’ is not an operator but examines the next  
token.

Bertram

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 19, 2009, 9:04am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/4 "2009-08-19T09:04:12Z")

</div>

Bertram S. wrote:

> Ugh, I did not know that `y’ is a method.

Only if you “require ‘yaml’”

One other thing to note. Deciding whether something is a local variable  
happens at parse time, independent of whether the line is actually  
excuted or not. So:

if false  
x = 123  
end  
p defined? x # “local\_variable”  
p x # nil

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 20, 2009, 1:49am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/5 "2009-08-20T01:49:55Z")

</div>

Sebastian H. wrote:

> Am Mittwoch 19 August 2009 04:22:18 schrieb Derek S.:
> 
> > My goal is to see if a table exists by testing the var holding the data  
> > is true/defined or not. Will you help?
> 
> true and defined are two very different things. Take this snippet as an  
> example:
> 
> x = 42  
> y = false  
> z = nil
> 
> Here the variables x, y and z are defined. The variables a, b and  
> foobarbaz  
> are not. In short: once you assign a value to a variable that variable  
> is  
> defined - no matter whether that value was true or not. If you want to  
> check  
> whether the value a variable holds is true or not, do that. Don’t use  
> defined?.  
> In your case you can just do if table\_exists which even reads more  
> naturally.
> 
> HTH,  
> Sebastian

Hey Seb,

Your advice is always welcome and respected. But Iam still confused,  
see below.  
The table derek does NOT exist of course.

table\_exists = db.execute(“select name from sqlite\_master where  
type=‘table’ and tbl\_name=‘derek’”)

if table\_exists  
p “yes”, “#{table\_exists}”  
end  
if table\_exists == “”  
p “yes”  
else  
p “no”  
end

**OUTPUT**

$ ruby mail\_log\_miner.rb  
“yes”  
“”  
“no”

With a table that does exist using same if clauses.

ruby mail\_log\_miner.rb  
“yes”  
“lines”  
“no”

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 20, 2009, 2:08am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/6 "2009-08-20T02:08:57Z")

</div>

On Aug 19, 2009, at 19:49, Derek S. wrote:

> if table\_exists == “”  
> p “yes”  
> else  
> p “no”  
> end
> 
> **OUTPUT**
> 
> $ ruby mail\_log\_miner.rb  
> “yes”

The variable “table\_exists” is not nil and not false. It is probably  
an empty array of rows, if you’re using the sqlite database adapter.

> “”

The stringified version of the variable “table\_exists” is an empty  
string. This is what you would expect if “table\_exists” is an empty  
array:

irb(main):001:0\> [].to\_s  
=\> “”

Putting “#{foo}” inside a string basically says "call .to\_s on the  
object and concatenate the results with the beginning and end of this  
string. In other words:

“#{table\_exists}”

is the same as

“” + table\_exists.to\_s + “”

If you want to see a better representation of an object, just call “p  
object” or “puts object.inspect”

> “no”

No, "table\_exists’ is not literally an empty string. You wouldn’t  
expect that, however, since db.execute doesn’t return strings, but  
arrays.

> With a table that does exist using same if clauses.
> 
> ruby mail\_log\_miner.rb  
> “yes”

Yes, table\_exists is not false and not nil.

> “lines”

“#{table\_exists}” is “lines”, i.e. table\_exists.to\_s is lines.

table\_exists is probably [“lines”], i.e. an array with 1 element, the  
string “lines”.

irb(main):002:0\> [“lines”].to\_s  
=\> “lines”

> “no”

No, “table\_exists” is not literally an empty string.

I think what you want is more along these lines:

# Don’t call it “table\_exists” because that implies the value is a

# boolean, when in fact it will be an array

table\_array = db.execute(“select name from sqlite\_master where  
type=‘table’ and tbl\_name=‘derek’”)

if table\_array.size \> 0  
p “yes”, table\_array  
end

if not table\_array.empty?  
p “yes”  
else  
p “no”  
end

Ben

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [August 20, 2009, 2:22am UTC](https://www.ruby-forum.com/t/if-defined-on-var/173990/7 "2009-08-20T02:22:20Z")

</div>

> I think what you want is more along these lines:
> 
> # Don’t call it “table\_exists” because that implies the value is a
> 
> # boolean, when in fact it will be an array
> 
> table\_array = db.execute(“select name from sqlite\_master where  
> type=‘table’ and tbl\_name=‘derek’”)
> 
> if table\_array.size \> 0  
> p “yes”, table\_array  
> end
> 
> if not table\_array.empty?  
> p “yes”  
> else  
> p “no”  
> end
> 
> Ben

Amen Ben! I understand completely. Thank you!  
Im still new to Ruby, coming from Perl.
