Robert Dober
3/29/2006 8:09:00 AM
On 3/29/06, vsv <gm.vlkv@gmail.com> wrote:
>
> Could someone explain me, please, why Range with one element is
> iterated inconsistently when this element is empty string:
>
> $ irb --simple-prompt
> >> ('a'..'a').to_a
> => ["a"]
> >> (1..1).to_a
> => [1]
> ## but:
> >> ("".."").to_a
> => []
> >> ("".."").each{|s| puts 'Inside'}
> => ""..""
> Why to_a returns empty [] and each block never executed?
> Is it bug or feature?
> Any reasonable explanation?
>
> thanks
> Sergey
>
>
>
Sergey
this looks like a bug to me
as far as I recall the definition of a range,
"" is part of the range ("".."")
I verified with irb and
("".."") === ""
returns true
"" <=> "" returns 0 as expected,
"".succ => "" which is not quite conclusive for me, but see below
anyway it seems inconsistent that
("".."").to_a.include?("") returns false
while
("".."") === "" returns true
No I have mimicked the bahvior with
class NilClass
def succ; nil; end
def <=>(other); 0; end
end
Guess what
(nil..nil).to_a returns ??
[nil] as expected
the behavior you described clearly seems to be a bug.
Regards
Robert
--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
- Albert Einstein