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.
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.
Well, the documentation says "non-nil items", and that means things
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.
> |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.
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.
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
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.
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.
> |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?
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.
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
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.