From: Shin-ichiro Hara Date: 2000-06-09T03:27:21+09:00 Subject: [ruby-list:23121] Re: making Array 原です。 |さくです。 |"1-3,5,7-9,16-17" |のような文字列(Niftyの会議室巡回コマンドの引数ですが)を、この例であれば |[1, 2, 3, 5, 7, 8, 9, 16, 17] |というArrayにするシンプルな方法はないでしょうか? シンプルではないが、由緒正しい方法で、、、 class Foo def initialize(mes) @s = mes.scan(/\d+|,|-/) end def self.compile(mes) new(mes).expr end def expr t = term while @s[0] == "," @s.shift t += term end t end private def term f = factor if @s[0] == "-" @s.shift (f .. factor).to_a else [f] end end def factor @s.shift.to_i end end Foo.compile("1-3,5,7-9,16-17") #=> [1, 2, 3, 5, 7, 8, 9, 16, 17]