Range oddity

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

Hi --

···

On Sat, 3 Dec 2005, Adam Shelly 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!

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

···

On 12/3/05, Adam Shelly <adam.shelly@gmail.com> 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?

oops, my bad.

···

On 12/2/05, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Sat, 3 Dec 2005, Adam Shelly 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!