From: "Mauricio Fernández" Date: 2002-08-22T04:27:06+09:00 Subject: Re: Puzzeled by Range object On Thu, Aug 22, 2002 at 01:03:07AM +0900, ts wrote: > >>>>> "R" == Robert McGovern writes: > > R> However the second, simply printed nothing. Am I wrong to expect that a > R> range can work for both up and down? > > Range can work only up. > > Except for Fixnum and Numeric, Range#each use #succ and it's difficult to > define the inverse of #succ for any type > What about this? batsman@kodos:~/src/rubylang$ cat myrange.rb class Range def each if (first <=> last) == -1 # proceed in normal order a = first while ( a != last ) yield a a = a.succ end yield last unless exclude_end? else # reverse! a = last a = a.succ if exclude_end? while ( a != first ) yield a a = a.succ end end end end l = [ "(1..3).each { |x| puts x }", "(-3..1).each { |x| puts x }", "(1...3).each { |x| puts x }", "(-3...1).each { |x| puts x }" ] l.each { |x| puts "Executing #{x}"; eval x } batsman@kodos:~/src/rubylang$ ruby myrange.rb Executing (1..3).each { |x| puts x } 1 2 3 Executing (-3..1).each { |x| puts x } -3 -2 -1 0 1 Executing (1...3).each { |x| puts x } 1 2 Executing (-3...1).each { |x| puts x } -3 -2 -1 0 batsman@kodos:~/src/rubylang$ Script terminado (Wed Aug 21 21:27:04 2002 ) It will surely break something, anybody cares to test it? -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Because I don't need to worry about finances I can ignore Microsoft and take over the (computing) world from the grassroots. -- Linus Torvalds