[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

find_all wierdness

Oleg Kh

7/28/2006 7:47:00 PM

My problem is as follows:

book.chapters.find_all{ |chapter| !chapter.pages.empty? }.size

Theoretically it should return a number of chapters that have pages in
them. However it doesn't do that at all. It simply returns a number of
all chapters.

On the same note:

book.chapters.find_all { |chapter| chapter.pages.empty? }.size returns
the same thing as above.

Identically

book.chapters.find_all { |chapter| 1 == 2 }.size still returns same
thing (shouldn't that be zero now???)

And finally

book.chapters.find_all{ my ass }.size returns exactly the same thing!!!
Seems that whatever is in brackets is simply ignored.

Any ideas why it does that? BTW, it seems to work just fine for
numerical arrays.

Thanks.

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

3 Answers

Kenosis

7/28/2006 8:33:00 PM

0


Oleg Kh wrote:
> My problem is as follows:
>
> book.chapters.find_all{ |chapter| !chapter.pages.empty? }.size
>
> Theoretically it should return a number of chapters that have pages in
> them. However it doesn't do that at all. It simply returns a number of
> all chapters.
>
> On the same note:
>
> book.chapters.find_all { |chapter| chapter.pages.empty? }.size returns
> the same thing as above.
>
> Identically
>
> book.chapters.find_all { |chapter| 1 == 2 }.size still returns same
> thing (shouldn't that be zero now???)
>
> And finally
>
> book.chapters.find_all{ my ass }.size returns exactly the same thing!!!
> Seems that whatever is in brackets is simply ignored.
>
> Any ideas why it does that? BTW, it seems to work just fine for
> numerical arrays.
>
> Thanks.
>
> --
> Posted via http://www.ruby-....
Can you send any more context for your use? I've used find_all on many
types of arrays w/ .size appended to a block and have no problems with
it. Just ran a couple of IRB tests too and all's well. Could it be
that your book.chapters is really empty? Have you tried adding a print
to your block to ensure its ever being called. Notice that you change
the block contents and always get the same result: this implies to me
the block is not the issue but what the enumerable has in it, if
anything is.

Cheers,

Ken

Daniel DeLorme

7/30/2006 3:57:00 PM

0

Oleg Kh wrote:
> My problem is as follows:
>
> book.chapters.find_all{ |chapter| !chapter.pages.empty? }.size
>
> Theoretically it should return a number of chapters that have pages in
> them. However it doesn't do that at all. It simply returns a number of
> all chapters.

Try to use select instead of find_all. In this case I think you are actually
triggering ActiveRecord's find_all instead of the Enumerable#find_all that you
were expecting. book.chapters is not an array but an association object. The
ActiveRecord version is defined as:
Project#milestones.find_all(conditions)
So in your case, you don't give any conditions as parameters and the method just
ignores the block and returns all chapters. Sometimes Rails magic can bite you
in the ass.

Daniel

Oleg Kh

8/1/2006 8:42:00 PM

0

Daniel DeLorme wrote:
> Try to use select instead of find_all. In this case I think you are
> actually
> triggering ActiveRecord's find_all instead of the Enumerable#find_all
> that you
> were expecting. book.chapters is not an array but an association object.
> The
> ActiveRecord version is defined as:
> Project#milestones.find_all(conditions)
> So in your case, you don't give any conditions as parameters and the
> method just
> ignores the block and returns all chapters. Sometimes Rails magic can
> bite you
> in the ass.
>
> Daniel


Thank you Daniel. That was it.

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