# Test to see if a variable exists

**URL:** <https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970>\
**Category:** Ruby\
**Created:** [June 15, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970 "2007-06-15T22:37:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 15, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970/1 "2007-06-15T22:37:09Z")

</div>

So I am stealing all these identities and storing everything in a big  
array of objects stolen\_identity, and having the index be social  
security numbers:

ss[34323843] = Stolen\_identity.new

But say I pick up a paycheck stub and it has 3428294. How do I know if  
I already have it? Does  
if (exist ss[3428294]) then … end  
work? How can I see if a variable exists?

Thanks.

(please include your social security with your answer)

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 15, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970/2 "2007-06-15T23:31:14Z")

</div>

On Jun 15, 2:36 pm, “Colin S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I am stealing all these identities and storing everything in a big  
> array of objects stolen\_identity, and having the index be social  
> security numbers:
> 
> ss[34323843] = Stolen\_identity.new
> 
> But say I pick up a paycheck stub and it has 3428294. How do I know if  
> I already have it? Does  
> if (exist ss[3428294]) then … end  
> work? How can I see if a variable exists?

Arrays (and Hashes) in Ruby are sparse - they only store the values  
you actually enter. However, there are some limitations on the size of  
indices for arrays:

C:\>irb

irb(main):001:0\> ss = []  
=\> []

irb(main):002:0\> ss[884\_56\_2103] = “I HAVE YOU NOW”  
ArgumentError: index too big  
from (irb):2:in `[]=’  
from (irb):2

So, you probably want to use a Hash, which has no such limitation.  
With both Arrays and Hashes, supplying a key or index which has no  
stored value will return a nil object:

irb(main):003:0\> ss = {}  
=\> {}

irb(main):004:0\> ss[884\_56\_2103] = “I HAVE YOU NOW”  
=\> “I HAVE YOU NOW”

irb(main):005:0\> ss[912\_34\_3552]  
=\> nil

In Ruby, nil and false are both non-truth values for the purposes of  
boolean logic. Additionally (and slightly more cleanly), you can ask a  
Hash directly if it has an entry for a specific key:

irb(main):006:0\> if ss.has\_key?( 884\_56\_2103 ) then  
irb(main):007:1\* puts “I’m done with this guy”  
irb(main):008:1\> else  
irb(main):009:1\* puts “Sweet new information!”  
irb(main):010:1\> end  
I’m done with this guy

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 15, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970/3 "2007-06-15T23:59:17Z")

</div>

On Jun 15, 2007, at 4:36 PM, Colin S. wrote:

> Thanks.
> 
> (please include your social security with your answer)

Using a hash is the way to go here. You’ve already been advised to do  
that. But here is an example that may give you some additional insight.

`
class Identity
    def initialize(ss)
       @ss = ss
    end
    def process
       puts "#{@ss}: I've been stolen!"
    end
end
Stolen = {}
ss1 = ‘123-12-1234’
ss2 = ‘789-78-7890’
Stolen[ss1] = Identity.new(ss1)
One way
Stolen[ss1].process if Stolen[ss1] # process runs
Stolen[ss2].process if Stolen[ss2] # process does not run
Another way
Stolen[ss1].process rescue nil # process runs
Stolen[ss2].process rescue nil # process does not run
`

There are many other ways, but they all rely on same thing: that  
Stolen[ss2] returns nil and nil is treated as false in boolean  
expressions.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 16, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970/4 "2007-06-16T15:05:35Z")

</div>

On Jun 15, 2007, at 4:58 PM, Morton G. wrote:

> > I already have it? Does
> 
> Stolen = {}  
> Stolen[ss1].process rescue nil # process runs

An array could indeed work. Think about the structure of a SSN:  
377-23-4736 (not a real one)  
That looks like a multi-dimensional array to me.  
each part is limited: 3 digits, 2 digits, 4 digits.  
Much like IPv4 this can produce a lot of distinct numbers.  
These smaller parts are easier to process, possibly less overhead.

---

<div class="post-metadata">

**Author:** ![banry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@banry](https://www.ruby-forum.com/u/banry)\
**Post date:** [June 16, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/test-to-see-if-a-variable-exists/106970/5 "2007-06-16T17:44:58Z")

</div>

On Jun 16, 2007, at 9:03 AM, John J. wrote:

> > > ss[34323843] = Stolen\_identity.new
> > 
> > ```
> > puts "#{@ss}: I've been stolen!"
> > 
> > ```
> > 
> > Stolen[ss1].process if Stolen[ss1] # process runs
> > 
> > Regards, Morton
> 
> An array could indeed work. Think about the structure of a SSN:  
> 377-23-4736 (not a real one)  
> That looks like a multi-dimensional array to me.  
> each part is limited: 3 digits, 2 digits, 4 digits.  
> Much like IPv4 this can produce a lot of distinct numbers.  
> These smaller parts are easier to process, possibly less overhead.

I don’t doubt that Identity objects could be indexed into some kind  
of array, but I do think the hash approach is simpler and easier to  
implement. I’d be happy to be convinced otherwise. How about posting  
some code to prove your point?

Regards, Morton
