Forum: Ruby Fun with ranges!

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.
91ac0ea6ae5c8cd67e2ed455b43ffbee?d=identicon&s=25 sgentle (Guest)
on 2005-11-26 00:51
(Received via mailing list)
So I was messing around today trying to find out whether there was a
viable way to cram unbounded lists into the Range implementation when
I stumbled on this...

irb(main):001:0> inf = 1.0/0.0
=> Infinity

irb(main):002:0> 1..inf
=> 1..Infinity

Hoorah! But not just that.

irb(main):003:0> 'a'..inf
=> "a"..Infinity

Hold on a second, I said to myself, that doesn't make any sense... inf
is a Float and "a" is a string! That's when I discovered ruby's
dastardly secret:

irb(main):004:0> 'a' <=> 3
=> false
irb(main):005:0> 3 <=> 'a'
=> nil

And hence:

irb(main):006:0> 'a'..3
=> "a"..3
irb(main):007:0> 3..'a'
ArgumentError: bad value for range
        from (irb):7

Zounds,

Sam
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-26 01:28
(Received via mailing list)
Hi --

On Sat, 26 Nov 2005, Sam Gentle wrote:

> Hoorah! But not just that.
> irb(main):005:0> 3 <=> 'a'
> => nil
>
> And hence:
>
> irb(main):006:0> 'a'..3
> => "a"..3
> irb(main):007:0> 3..'a'
> ArgumentError: bad value for range
>        from (irb):7

What version of Ruby are you using?

$ ruby -ve '"a"..3'
ruby 1.8.3 (2005-09-21) [powerpc-darwin8.3.0]
-e:1: warning: useless use of .. in void context
-e:1: bad value for range (ArgumentError)


David
91ac0ea6ae5c8cd67e2ed455b43ffbee?d=identicon&s=25 sgentle (Guest)
on 2005-11-26 01:36
(Received via mailing list)
On 11/26/05, David A. Black <dblack@wobblini.net> wrote:
> What version of Ruby are you using?
>
> $ ruby -ve '"a"..3'
> ruby 1.8.3 (2005-09-21) [powerpc-darwin8.3.0]
> -e:1: warning: useless use of .. in void context
> -e:1: bad value for range (ArgumentError)

C:\>ruby -ve '"a"..3'
ruby 1.8.2 (2005-02-01) [i386-mswin32]
-e:1: warning: useless use of .. in void context

Strange...

weill samg$ ruby -ve '"a"..3'
ruby 1.8.0 (2003-08-04) [i686-linux]
-e:1: warning: useless use of .. in void context
-e:1: bad value for range (ArgumentError)

Double-strange... I wonder if it's a windows thing or a version thing
(and if so why it only pops up somewhere between 1.8.0 and 1.8.3).

Sam
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-26 01:44
(Received via mailing list)
Hi --

On Sat, 26 Nov 2005, Sam Gentle wrote:

> -e:1: warning: useless use of .. in void context
>
> Strange...
>
> weill samg$ ruby -ve '"a"..3'
> ruby 1.8.0 (2003-08-04) [i686-linux]
> -e:1: warning: useless use of .. in void context
> -e:1: bad value for range (ArgumentError)
>
> Double-strange... I wonder if it's a windows thing or a version thing
> (and if so why it only pops up somewhere between 1.8.0 and 1.8.3).

Interesting -- it seems to be a 1.8.2 thing.


David
This topic is locked and can not be replied to.