Ruby – 為何 Range 不能大到小?

(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

(10..1).to_a
=> []

所以

for i in 1..10 do
puts i
end

會印 1 到 10

for i in 10..1 do
puts i
end

卻不會印 10 到 1

雖然可以用 10.downto(1)

不過還是太 Orz 了….


  • 留言者: Duncan
  • Email:
  • 網址:
  • 日期: 2005-09-27 11:48:25

或許 Python 更適合你。


  • 留言者: JiaYun
  • Email:
  • 網址:
  • 日期: 2005-09-27 13:49:19

Python:

for i in reversed(xrange(1,11)):

print i

這學期有個 project 有點想用 Ruby on Rails 做

如果 Python 的話也許可以用 http://www.djangoproject.com/

不過有辦法學那麼多嗎…. Orz


  • 留言者: T55555
  • Email:
  • 網址:
  • 日期: 2005-09-28 02:36:30

Please check the ruby-talk: 8993 and 39342

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/8993

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/39342

And be aware of Range using on Array index:

For example:

ary = %w[a b c d e]

ary[1..-1] # => [‘b’, ‘c’, ‘d’, ‘e’]

If we *assume* Range support from “big” to “small”,

the example 1..-1 will like have 1, 0, -1 elements

and ary[1..-1] will returns [‘b’, ‘a’, ‘e’]

but the current design is not like this way …

The main raison Range do not support from “big” to “small” is

because the Range is define base on object’s “succ”( or alias “next”) and “”

BTW: You could “redefine” the Range to behaviour as you like.


  • 留言者: T55555
  • Email:
  • 網址:
  • 日期: 2005-09-28 02:41:17

“succ” and ” < = > ”

後面是 *宇宙飛碟* operator.


  • 留言者: T55555
  • Email:
  • 網址:
  • 日期: 2005-09-28 02:55:29

Not prefect at all ( need to take care of === also, string Range … etc),

but it seems to work for what you expect.

class Range

alias old_each each

def each(&block)

return old_each(&block) unless first > last

first.downto(last) { |e| yield e }

return self

end

end

p (1..10).to_a

p (10..1).to_a

(10..1).each { |e| p e }


  • 留言者: JiaYun
  • Email:
  • 網址:
  • 日期: 2005-09-28 03:18:35

也許它可以加個 pred 或 prev, 再把用在 array 的情形改一下, 功能上會更對稱一點….

我好像 柯南 – 引爆摩天樓 裡那個超執著對稱的森谷帝二教授…. XD


  • 留言者: Duncan
  • Email:
  • 網址:
  • 日期: 2005-09-28 16:19:31

可以用 range(1, 11)[::-1] 或是 xrange(10, 0, -1)。


  • 留言者: JiaYun
  • Email:
  • 網址:
  • 日期: 2005-09-29 01:18:24

我想搭紅寶石列車,Duncan 集團誘惑我乘巨蟒….

Array Slice

這幾天在看 Programming Ruby (2nd. Ed.)

看到 Array 部分時,好奇試了這些個東西

a = ["a", "b", "c", "d", "e"]
a[3, -2]
a[3..1]
a[-1..1]
a[-1..-3]

結果

a[3, -2] => nil
a[3..1] => []
a[-1..1] => []
a[-1..-3] => []

我想要是我來設計,大概會給它弄成這樣

a[3, -2] => ["d", "c"]
a[3..1] => ["d", "c", "b"]
a[-1..1] => ["e", "d", "c", "b"]
a[-1..-3] => ["e", "d", "c"]

因為既然負的 index 是由最後往前數(a[-1] => “e”),我會覺得取好幾個也要有由後往前取的功能 😛


  • 留言者: T55555
  • Email:
  • 網址:
  • 日期: 2005-09-01 19:21:15

Welcome to the Ruby World.

The [] rule is also apply to []=

So, if a = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

and if a[3..1] => [‘d’, ‘c’, ‘b’]

What you would like to see if you write something like

a[3..1] = [1, 2, 3, 4] ?

a will become to [‘a’, 4, 3, 2, 1, ‘e’] ?

One good thing about Ruby is everything are open.

Even the core library.

There are also calibre and facets project to change the array index to the way they like.

I can easily change the length negative value behavior as you like ( the Range one, leave to yourself to do it) , for example :

class Array

alias_method :old, :"[]"

def [](*args)  # define my new [] method for Array

begin

old(*args) unless args.length == 2 && args.last < 0

(0...-args.last).inject([]) { |a, i| a << self.slice(args.first - i) }

rescue

old(*args)

end

end

end



a = %w[a b c d e] #=> a = ['a', 'b', 'c', 'd', 'e']

a[3, -2]          #=> ['d', 'c']

So, if you don’t like the default way, change to what you like.

On Ruby, you could even change 1+1 to become 3 if you like. 🙂


  • 留言者: JiaYun
  • Email:
  • 網址:
  • 日期: 2005-09-01 20:41:12

Yes, I will let a be [‘a’, 4, 3, 2, 1, ‘e’] after a[3..1] = [1, 2, 3, 4].

And I finished the homework, but only [] not []=.

class Array

alias_method :old, :"[]"

def [](*args)  # define my new [] method for Array

begin

if (args.length == 2 && args.last < 0)

(0...-args.last).inject([]) { |a, i| a << self.slice(args.first - i) }

elsif (args.length == 1 && args.first.class == Range)

range = args.first

first = range.first < 0 ? range.first + self.length : range.first

last = range.last < 0 ? range.last + self.length : range.last

if (first > last)

length = (first - last).abs

length += 1 unless range.exclude_end?

(0...length).inject([]) { |a, i| a << self.slice(range.first - i) }

else

old(*args)

end

else

old(*args)

end



rescue

old(*args)

end

end

end



a = %w[a b c d e]

a[-1..-3] #=> ["e", "d", "c"]

a[-1...-3] #=> ["e", "d"]

a[-1..1] #=> ["e", "d", "c", "b"]

a[-1...1] #=> ["e", "d", "c"]

a[4..2] #=> ["e", "d", "c"]

a[4...2] #=> ["e", "d"]


  • 留言者: T55555
  • Email: ruby55555@gmail.com
  • 網址:
  • 日期: 2005-09-02 00:09:54

Ooops, I found my bug …

The line

old(*args) unless args.length == 2 && args.last < 0

should be like

return old(*args) unless args.length == 2 && args.last < 0