Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array#nitems and Object#nil?

1 view
Skip to first unread message

Eliah Hecht

unread,
Mar 11, 2005, 1:57:32 AM3/11/05
to
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.


George Ogata

unread,
Mar 12, 2005, 2:14:32 PM3/12/05
to
Eliah Hecht <eliah...@gmail.com> writes:

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

unread,
Mar 12, 2005, 2:52:35 PM3/12/05
to
Eliah Hecht <eliah...@gmail.com> writes:

Well, the documentation says "non-nil items", and that means things

Yukihiro Matsumoto

unread,
Mar 13, 2005, 7:37:24 AM3/13/05
to
Hi,

In message "Re: Array#nitems and Object#nil?"


on Fri, 11 Mar 2005 15:57:32 +0900, Eliah Hecht <eliah...@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

unread,
Mar 13, 2005, 8:38:10 AM3/13/05
to
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

unread,
Mar 13, 2005, 7:43:45 PM3/13/05
to
On Sun, 13 Mar 2005 21:37:24 +0900, Yukihiro Matsumoto
<ma...@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.


David A. Black

unread,
Mar 13, 2005, 7:53:07 PM3/13/05
to
Hi --

nil is an object, so any other object that says "I am the object nil"
(which is what nil? tests for) is giving false information. You can
rig an object to do that, just as you can rig, say, the number 3 to
tell you that it's equal to the number 4, but it's still basically
untrue.


David

--
David A. Black
dbl...@wobblini.net


Yukihiro Matsumoto

unread,
Mar 13, 2005, 8:19:26 PM3/13/05
to
Hi,

In message "Re: Array#nitems and Object#nil?"

on Mon, 14 Mar 2005 09:43:45 +0900, Eliah Hecht <eliah...@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

unread,
Mar 15, 2005, 10:58:12 AM3/15/05
to
Hi,

In message "Re: Array#nitems and Object#nil?"

on Tue, 15 Mar 2005 22:58:07 +0900, Florian Gross <fl...@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

unread,
Mar 17, 2005, 11:29:44 AM3/17/05
to
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

unread,
Mar 17, 2005, 12:12:23 PM3/17/05
to
Hi,

In message "Re: Array#nitems and Object#nil?"

on Fri, 18 Mar 2005 01:34:49 +0900, Florian Gross <fl...@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.


Bertram Scharpf

unread,
Mar 18, 2005, 10:07:06 AM3/18/05
to
Hi,

That's 3 years per effective code line, if you add the block
feature to Array#nitems.

I appended it to my demanded and regarded private patch list at
<http://projects.bertram-scharpf.de/tmp/bertram-scharpf-ruby.patch>.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de


Yukihiro Matsumoto

unread,
Jun 7, 2005, 11:30:29 PM6/7/05
to
Hi,

In message "Re: Array#nitems and Object#nil?"

on Sat, 19 Mar 2005 00:07:06 +0900, Bertram Scharpf <li...@bertram-scharpf.de> writes:

|That's 3 years per effective code line, if you add the block
|feature to Array#nitems.

Today, I happened to re-discover this mail, and found it reasonable.
Array#nitems will be able to take a block to count items specified by
the block (CVS HEAD).

matz.


0 new messages