Tuesday, September 27, 2005

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 集團誘惑我乘巨蟒....

No comments:

Post a Comment