# Range oddity

**URL:** <https://www.ruby-forum.com/t/range-oddity/94871>\
**Category:** Ruby\
**Created:** [December 2, 2005, 11:23pm UTC](https://www.ruby-forum.com/t/range-oddity/94871 "2005-12-02T23:23:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.shelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@adam.shelly](https://www.ruby-forum.com/u/adam.shelly)\
**Post date:** [December 2, 2005, 11:23pm UTC](https://www.ruby-forum.com/t/range-oddity/94871/1 "2005-12-02T23:23:41Z")

</div>

I was playing around ranges of strings after that post about

> ‘1’…‘10’.include? '2 #=\> false  
> I found something else strange when I made a typo:

irb(main):001:0\> a = 1…10.to\_a  
(irb):1: warning: default `to\_a’ will be obsolete  
ArgumentError: bad value for range  
from (irb):1

irb(main):004:0\> a = ‘1’…‘10’.to\_a  
=\> “1”…[“10”]  
irb(main):005:0\> a.each{|i| p i}  
TypeError: cannot convert Array into String  
from (irb):5:in `each' from (irb):5 irb(main):006:0> a => "1"..["10"] irb(main):007:0> a.to_a TypeError: cannot convert Array into String from (irb):7:in`each’  
from (irb):7:in `to\_a’  
from (irb):7

irb(main):015:0\> r=‘1’…[10]  
=\> “1”…[10]  
irb(main):016:0\> r.each{|i| p i}  
TypeError: cannot convert Array into String  
from (irb):16:in `each' from (irb):16 irb(main):017:0> r.include? [5] NoMethodError: undefined method`\>’ for false:FalseClass  
from (irb):17:in `include?’  
from (irb):17

Why can I create a Range where one end is a String and the other is an  
Array? I can’t do anything with it. I get an error if I create an  
Fixnum…Array range, why don’t I get one for the String…Array?

-Adam

---

<div class="post-metadata">

**Author:** ![adam.shelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@adam.shelly](https://www.ruby-forum.com/u/adam.shelly)\
**Post date:** [December 3, 2005, 3:52am UTC](https://www.ruby-forum.com/t/range-oddity/94871/2 "2005-12-03T03:52:03Z")

</div>

oops, my bad.

---

<div class="post-metadata">

**Author:** ![adam.shelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@adam.shelly](https://www.ruby-forum.com/u/adam.shelly)\
**Post date:** [December 2, 2005, 11:31pm UTC](https://www.ruby-forum.com/t/range-oddity/94871/3 "2005-12-02T23:31:47Z")

</div>

Hi –

On Sat, 3 Dec 2005, Adam S. wrote:

> Why can I create a Range where one end is a String and the other is an  
> Array? I can’t do anything with it. I get an error if I create an  
> Fixnum…Array range, why don’t I get one for the String…Array?

That seems to be fixed in 1.8.3:  
$ ruby -ve ‘a = “1”…[“10”]’  
ruby 1.8.3 (2005-09-21) [powerpc-darwin8.3.0]  
-e:1: bad value for range (ArgumentError)

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

---

<div class="post-metadata">

**Author:** ![adam.shelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@adam.shelly](https://www.ruby-forum.com/u/adam.shelly)\
**Post date:** [December 3, 2005, 1:06pm UTC](https://www.ruby-forum.com/t/range-oddity/94871/4 "2005-12-03T13:06:52Z")

</div>

On 12/3/05, Adam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why can I create a Range where one end is a String and the other is an  
> Array? I can’t do anything with it. I get an error if I create an  
> Fixnum…Array range, why don’t I get one for the String…Array?

If anyone’s curious about the internals behind this - I had a look  
when I had a similar problem. Range checks for the validity of a…b by  
calling a \<=\> b and ensuring that the return value isn’t nil. You can  
see the results yourself by trying that with different values. For  
some reason, in 1.8.2 strings return false instead of nil when  
compared with a value they don’t know how to handle. This sneaks past  
Range’s test.

As David mentions, this seems to have been fixed in 1.8.3.

Sam
