Range oddity

A

Adam Shelly

I was playing around ranges of strings after that post about
'1'..'10'.include? '2 #=3D> false
I found something else strange when I made a typo:

irb(main):001:0> a =3D 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 =3D '1'..'10'.to_a
=3D> "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
=3D> "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=3D'1'..[10]
=3D> "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
 
D

David A. Black

Hi --

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
 
A

Adam Shelly

oops, my bad.

Hi --

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 =3D "1"..["10"]'
ruby 1.8.3 (2005-09-21) [powerpc-darwin8.3.0]
-e:1: bad value for range (ArgumentError)


David

--
David A. Black
(e-mail address removed)

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

Sam Gentle

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 <=3D> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top