# Range#to\_a, each - why inconsistent?

**URL:** <https://www.ruby-forum.com/t/range-to-a-each-why-inconsistent/89825>\
**Category:** Ruby\
**Created:** [March 29, 2006, 9:29am UTC](https://www.ruby-forum.com/t/range-to-a-each-why-inconsistent/89825 "2006-03-29T09:29:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vsv](https://www.ruby-forum.com/u/vsv)\
**Post date:** [March 29, 2006, 9:29am UTC](https://www.ruby-forum.com/t/range-to-a-each-why-inconsistent/89825/1 "2006-03-29T09:29:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vsv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vsv](https://www.ruby-forum.com/u/vsv)\
**Post date:** [March 29, 2006, 10:09am UTC](https://www.ruby-forum.com/t/range-to-a-each-why-inconsistent/89825/2 "2006-03-29T10:09:28Z")

</div>

On 3/29/06, vsv [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > (“”…“”).to\_a

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

---

<div class="post-metadata">

**Author:** ![vsv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/278dde/32.png) [@vsv](https://www.ruby-forum.com/u/vsv)\
**Post date:** [March 29, 2006, 10:11am UTC](https://www.ruby-forum.com/t/range-to-a-each-why-inconsistent/89825/3 "2006-03-29T10:11:23Z")

</div>

I initially thought it was because “”.succ == “” which is not correct,  
however I created a custom class that returns itself when succ is  
called:

class BadSucc  
include Comparable  
attr\_accessor :value  
def \<=\>(other)  
self.value \<=\> other.value  
end  
def succ  
self  
end  
end

But this did not seem to work  
irb(main):002:0\> b = BadSucc.new  
=\> #BadSucc:0x2dc49a8  
irb(main):003:0\> b.value = 1  
=\> 1  
irb(main):008:0\> (b…b).to\_a  
=\> [#\<BadSucc:0x2dc49a8 @value=1\>]

So it seems like it might be a bug to me or there is some other reason  
why (""…"").to\_a returns empty while (b…b).to\_a doesn’t.

Farrel
