Beginners question about Array#assoc

C

Chad Thatcher

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.
 
S

Stefano Crocco

Alle 20:52, venerd=C3=AC 8 dicembre 2006, Chad Thatcher ha scritto:
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) =3D=3D 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.

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

if an_array.assoc(tag_name) #the element has been found
=2E..
else #in this case assoc returned nil because no element was found
=2E..
end

I hope this answeres your question.

Stefano
 
D

Daniel Finnie

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
 
C

Chad Thatcher

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...
 
C

Chad Thatcher

Daniel said:
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 :D
 
E

Edwin Fine

Chad said:
Daniel said:
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 :D

Or you could write

unless masters.assoc(row["tag"])

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top