Forum: Ruby Range oddity

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
adam.shelly (Guest)
on 2005-12-03 00:23
(Received via mailing list)
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
dblack (Guest)
on 2005-12-03 00:31
(Received via mailing list)
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!
adam.shelly (Guest)
on 2005-12-03 04:52
(Received via mailing list)
oops, my bad.
sgentle (Guest)
on 2005-12-03 14:06
(Received via mailing list)
On 12/3/05, Adam S. <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
This topic is locked and can not be replied to.