[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array#nitems and Object#nil?

Eliah Hecht

3/11/2005 6:58:00 AM

I was working on an assignment for my Algorithms & Data Structures
class when I determined that I had a need to figure out whether an
array was (a) empty, or contained nothing but nils and things that I
want to consider nil, or (b) contained non-nil-esque items. So I was
pleasantly surprised to discover Array#nitems; I thought to myself,
"I'll just make nil-like things respond true to nil?". However, this
didn't seem to work: it appears that Array#nitems checks whether the
array elements == nil, not whether they respond true to nil?. It seems
to me that a nil?-based approach would be more productive, allowing
greater flexibility in terms of what counts as an item.

In case I'm being unclear, if I do
class Thing
def nil?
true
end
end
t = Thing.new
I think it should be the case that [t, nil, 7].nitems returns 1,
instead of 2 as it currently does.

-Eliah.


12 Answers

George Ogata

3/12/2005 7:15:00 PM

0

Eliah Hecht <eliahhecht@gmail.com> writes:

> I was working on an assignment for my Algorithms & Data Structures
> class when I determined that I had a need to figure out whether an
> array was (a) empty, or contained nothing but nils and things that I
> want to consider nil, or (b) contained non-nil-esque items. So I was
> pleasantly surprised to discover Array#nitems; I thought to myself,
> "I'll just make nil-like things respond true to nil?". However, this
> didn't seem to work: it appears that Array#nitems checks whether the
> array elements == nil, not whether they respond true to nil?. It seems
> to me that a nil?-based approach would be more productive, allowing
> greater flexibility in terms of what counts as an item.
>
> In case I'm being unclear, if I do
> class Thing
> def nil?
> true
> end
> end
> t = Thing.new
> I think it should be the case that [t, nil, 7].nitems returns 1,
> instead of 2 as it currently does.

Well, the documentation says "non-nil items", and that means things
that aren't nil. :) Checking for the exact value nil is quicker than
using a user-defined criterion. #any? and #all? are good for what
you're trying to do.

George Ogata

3/12/2005 7:53:00 PM

0

Eliah Hecht <eliahhecht@gmail.com> writes:

> I was working on an assignment for my Algorithms & Data Structures
> class when I determined that I had a need to figure out whether an
> array was (a) empty, or contained nothing but nils and things that I
> want to consider nil, or (b) contained non-nil-esque items. So I was
> pleasantly surprised to discover Array#nitems; I thought to myself,
> "I'll just make nil-like things respond true to nil?". However, this
> didn't seem to work: it appears that Array#nitems checks whether the
> array elements == nil, not whether they respond true to nil?. It seems
> to me that a nil?-based approach would be more productive, allowing
> greater flexibility in terms of what counts as an item.
>
> In case I'm being unclear, if I do
> class Thing
> def nil?
> true
> end
> end
> t = Thing.new
> I think it should be the case that [t, nil, 7].nitems returns 1,
> instead of 2 as it currently does.

Well, the documentation says "non-nil items", and that means things
that aren't nil. :) Checking for the exact value nil is quicker than
using a user-defined criterion. #any? and #all? are good for what
you're trying to do.



Yukihiro Matsumoto

3/13/2005 12:37:00 PM

0

Hi,

In message "Re: Array#nitems and Object#nil?"
on Fri, 11 Mar 2005 15:57:32 +0900, Eliah Hecht <eliahhecht@gmail.com> writes:

|In case I'm being unclear, if I do
|class Thing
| def nil?
| true
| end
|end
|t = Thing.new
|I think it should be the case that [t, nil, 7].nitems returns 1,
|instead of 2 as it currently does.

Nil is a nil is a nil. Defining nil? to be true doesn't make
something nil. It's just a false predicate.

matz.


Florian Gross

3/13/2005 1:38:00 PM

0

Yukihiro Matsumoto wrote:

> |In case I'm being unclear, if I do
> |class Thing
> | def nil?
> | true
> | end
> |end
> |t = Thing.new
> |I think it should be the case that [t, nil, 7].nitems returns 1,
> |instead of 2 as it currently does.
>
> Nil is a nil is a nil. Defining nil? to be true doesn't make
> something nil. It's just a false predicate.

On another note: Can we perhaps introduce Enumerable#count which returns
the number of elements for which the block is true? I know that we can
do .find_all { ... }.size, but constructing an Array of all elements
just to get its size seems wasteful.

Eliah Hecht

3/14/2005 12:44:00 AM

0

On Sun, 13 Mar 2005 21:37:24 +0900, Yukihiro Matsumoto
<matz@ruby-lang.org> wrote:
> Nil is a nil is a nil. Defining nil? to be true doesn't make
> something nil. It's just a false predicate.

In that case, what's the point of nil?? I'm not sure what you mean by
"It's just a false predicate"; an object that returns true for nil?
doesn't seem to be false-valued. Does nil? do anything in the
language, or is it just meant to be a convient way to check if
something == nil?
-Eliah.


dblack

3/14/2005 12:53:00 AM

0

Yukihiro Matsumoto

3/14/2005 1:19:00 AM

0

Hi,

In message "Re: Array#nitems and Object#nil?"
on Mon, 14 Mar 2005 09:43:45 +0900, Eliah Hecht <eliahhecht@gmail.com> writes:

|In that case, what's the point of nil??

It's a convenient (or better looking for someones' eyes) way to check
if something is nil, as you've guessed.

matz.


Yukihiro Matsumoto

3/15/2005 3:58:00 PM

0

Hi,

In message "Re: Array#nitems and Object#nil?"
on Tue, 15 Mar 2005 22:58:07 +0900, Florian Gross <flgr@ccan.de> writes:

|On another note: Can we perhaps introduce Enumerable#count which returns
|the number of elements for which the block is true? I know that we can
|do .find_all { ... }.size, but constructing an Array of all elements
|just to get its size seems wasteful.

enum.inject(0){|i,j| j ? i + 1 : i }

No array construction, bit harder to read than #count.

matz.


Florian Gross

3/17/2005 4:30:00 PM

0

Yukihiro Matsumoto wrote:

> |On another note: Can we perhaps introduce Enumerable#count which returns
> |the number of elements for which the block is true? I know that we can
> |do .find_all { ... }.size, but constructing an Array of all elements
> |just to get its size seems wasteful.
>
> enum.inject(0){|i,j| j ? i + 1 : i }
>
> No array construction, bit harder to read than #count.

Is it too rare to deserve a built-in short cut?

Yukihiro Matsumoto

3/17/2005 5:12:00 PM

0

Hi,

In message "Re: Array#nitems and Object#nil?"
on Fri, 18 Mar 2005 01:34:49 +0900, Florian Gross <flgr@ccan.de> writes:

|> enum.inject(0){|i,j| j ? i + 1 : i }
|>
|> No array construction, bit harder to read than #count.
|
|Is it too rare to deserve a built-in short cut?

I'm not sure. I've never had need for this operation before in the
10+ years of Ruby programming though.

matz.