# Beginners question about Array#assoc

**URL:** <https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823>\
**Category:** Ruby\
**Created:** [December 8, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823 "2006-12-08T20:52:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 8, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/1 "2006-12-08T20:52:32Z")

</div>

Hi, I am new to Ruby and still finding it very strange even though I am  
enjoying learning it immensely.

I have this code:

if an\_array.assoc(tag\_name) == nil  
…

and of course it doesn’t work because I either get an array back or a  
nil object. It makes it very difficult to have a simple if statement  
like the above that can handle both eventualities. I don’t need the  
array returned, I just need to check to see if the element exists to  
take a certain action.

What is the Ruby way of doing this?

Thanks,

Chad.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 8, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/2 "2006-12-08T21:02:01Z")

</div>

Alle 20:52, venerdÃ¬ 8 dicembre 2006, Chad T. ha scritto:

> like the above that can handle both eventualities. I don’t need the  
> array returned, I just need to check to see if the element exists to  
> take a certain action.
> 
> What is the Ruby way of doing this?
> 
> Thanks,
> 
> Chad.

In ruby everything evaluates to true in conditionals, except false and  
nil.  
So, you can do something like

if an\_array.assoc(tag\_name) #the element has been found  
…  
else #in this case assoc returned nil because no element was found  
…  
end

I hope this answeres your question.

Stefano

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 2:27am UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/3 "2006-12-09T02:27:15Z")

</div>

Thanks Stefano.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 2:38am UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/4 "2006-12-09T02:38:43Z")

</div>

It might be include? (the question mark is part of the method name)

An example of include?'s usage:  
irb(main):001:0\> array = [“a”, “b”, “c”]  
=\> [“a”, “b”, “c”]  
irb(main):002:0\> array.include? “a”  
=\> true  
irb(main):003:0\> array.include? “z”  
=\> false

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 2:42am UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/5 "2006-12-09T02:42:48Z")

</div>

Daniel F. wrote:

> It might be include? (the question mark is part of the method name)
> 
> An example of include?'s usage:  
> irb(main):001:0\> array = [“a”, “b”, “c”]  
> =\> [“a”, “b”, “c”]  
> irb(main):002:0\> array.include? “a”  
> =\> true  
> irb(main):003:0\> array.include? “z”  
> =\> false

Thing is its an array of arrays so I need to use the assoc method.

Anyway I figured it out, the code should have read:

if !masters.assoc(row[“tag”])

I was being dense and coding at a quarter to 2am on a friday night is  
never an advisable thing 😃

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 8:26am UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/6 "2006-12-09T08:26:53Z")

</div>

Chad T. wrote:

> Daniel F. wrote:
> 
> > It might be include? (the question mark is part of the method name)
> > 
> > An example of include?'s usage:  
> > irb(main):001:0\> array = [“a”, “b”, “c”]  
> > =\> [“a”, “b”, “c”]  
> > irb(main):002:0\> array.include? “a”  
> > =\> true  
> > irb(main):003:0\> array.include? “z”  
> > =\> false
> 
> Thing is its an array of arrays so I need to use the assoc method.
> 
> Anyway I figured it out, the code should have read:
> 
> if !masters.assoc(row[“tag”])
> 
> I was being dense and coding at a quarter to 2am on a friday night is  
> never an advisable thing 😃

Or you could write

unless masters.assoc(row[“tag”])

This seems to be more Rubyish practice from what I’ve seen.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [December 9, 2006, 2:39am UTC](https://www.ruby-forum.com/t/beginners-question-about-array-assoc/78823/7 "2006-12-09T02:39:12Z")

</div>

Actually, I am still having trouble. My code is:

res = dbh.query(“SELECT tag …blah blah”)  
masters = Array.new  
res.each\_hash do |row|  
p row[“tag”]

if masters.assoc(row[“tag”])  
newMaster = { “subtags” =\> Array.new, “master” =\> ‘a’ }  
masters.push [row[“tag”], newMaster]  
end  
…

And here is my output:

“852”  
./prep\_tags\_yaml\_02.rb:65: undefined method `[]’ for nil:NilClass  
(NoMethodError)  
from ./prep\_tags\_yaml\_02.rb:8

I tried populating the “masters” array with an init line like:

masters = [[“asdf”, “agf”], [“bb”, “behe”]]

and it works, but this doesn’t help as the array has top be empty to  
begin with. Puzzling…
