[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginners question about Array#assoc

Builder Chad

12/8/2006 7:53:00 PM

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.

--
Posted via http://www.ruby-....

26 Answers

Stefano Crocco

12/8/2006 8:00:00 PM

0

Alle 20:52, venerdì 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) == 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.
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

Builder Chad

12/9/2006 1:27:00 AM

0

Thanks Stefano.


--
Posted via http://www.ruby-....

Daniel Finnie

12/9/2006 1:37:00 AM

0

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


Chad Thatcher wrote:
> 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.
>

Builder Chad

12/9/2006 1:39:00 AM

0

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

--
Posted via http://www.ruby-....

Builder Chad

12/9/2006 1:43:00 AM

0

Daniel Finnie 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 :D

--
Posted via http://www.ruby-....

Edwin Fine

12/9/2006 7:27:00 AM

0

Chad Thatcher wrote:
> Daniel Finnie 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 :D

Or you could write

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

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

--
Posted via http://www.ruby-....

td

3/22/2012 9:45:00 PM

0

On Mar 22, 4:40 pm, Kirk McElhearn <kirkmc (at) mac (dot) com> wrote:
> On 2012-03-22 18:25:58 +0000, Thornhill <seth.l...@gmail.com> said:
>
> > I do not claim to have any clue of what SDG's finances are, but I
> > imagine they've decided to do this fundraising campaign because they
> > don't have a ton of cash on hand at the moment and they want to
> > minimize the risk of making this recording.
>
> I'd also speculate that recording these cantatas, which would finish
> the series, would allow them to release a complete box set, attracting
> new buyers who didn't want to pay for the original releases.

You are assuming, of course - HUGE assumption - that DG would be
willing to license the missing recordings from this series which they
issued on the yellow label.

These four are just four that were missing from the whole project as
set out.

TD

Terry

3/22/2012 10:52:00 PM

0

On Fri, 23 Mar 2012 07:28:15 +1100, graham wrote
(in article <BXLar.2693$BZ2.713@newsfe07.iad>):

>
> "Mark S" <markstenroos@yahoo.com> wrote in message
> news:8016fe30-3c17-452b-8556-84aacf8ab177@j14g2000vbc.googlegroups.com...
> On Mar 22, 11:25 am, Thornhill <seth.l...@gmail.com> wrote:
>
>>
>> I do not claim to have any clue of what SDG's finances are, but I
>> imagine they've decided to do this fundraising campaign because they
>> don't have a ton of cash on hand at the moment and they want to
>> minimize the risk of making this recording.
>
> I would think that JEG could self-finance these recordings if he
> wanted. He's clearly a very wealthy man.
> ----------------------
> Who is probably tight-fisted!
> Graham
>
>

Extraordinary statement, based on nothing! A piece of meanness.

--
Cheers!

Terry

td

3/22/2012 11:40:00 PM

0

On Mar 22, 4:28 pm, "graham" <g.ste...@shaw.ca> wrote:
> "Mark S" <markstenr...@yahoo.com> wrote in message
>
> news:8016fe30-3c17-452b-8556-84aacf8ab177@j14g2000vbc.googlegroups.com...
> On Mar 22, 11:25 am, Thornhill <seth.l...@gmail.com> wrote:
>
>
>
> > I do not claim to have any clue of what SDG's finances are, but I
> > imagine they've decided to do this fundraising campaign because they
> > don't have a ton of cash on hand at the moment and they want to
> > minimize the risk of making this recording.
>
> I would think that JEG could self-finance these recordings if he
> wanted. He's clearly a very wealthy man.
> ----------------------
> Who is probably tight-fisted!

People who have a lot of money rarely want to part with it. They would
far rather someone else ponied up for their pleasures. That way they
stay wealthy.

TD

td

3/22/2012 11:41:00 PM

0

On Mar 22, 6:52 pm, Terry <b...@clown.invalid> wrote:
> On Fri, 23 Mar 2012 07:28:15 +1100, graham wrote
> (in article <BXLar.2693$BZ2....@newsfe07.iad>):
>
>
>
>
>
>
>
>
>
>
>
> > "Mark S" <markstenr...@yahoo.com> wrote in message
> >news:8016fe30-3c17-452b-8556-84aacf8ab177@j14g2000vbc.googlegroups.com...
> > On Mar 22, 11:25 am, Thornhill <seth.l...@gmail.com> wrote:
>
> >> I do not claim to have any clue of what SDG's finances are, but I
> >> imagine they've decided to do this fundraising campaign because they
> >> don't have a ton of cash on hand at the moment and they want to
> >> minimize the risk of making this recording.
>
> > I would think that JEG could self-finance these recordings if he
> > wanted. He's clearly a very wealthy man.
> > ----------------------
> > Who is probably tight-fisted!
> > Graham
>
> Extraordinary statement, based on nothing! A piece of meanness.

Unfortunately, it's accurate.

TD