From: shugo@... (MAEDA Shugo) Date: 1997-09-18T20:55:06+09:00 Subject: [ruby-list:4422] Re: nif2news.rb 前田です。 In message "[ruby-list:4420] Re: nif2news.rb" Yukihiro Matsumoto wrote: |その時点での $~ の値を取り出して,引数として渡せば,再代入す |る事によってコンテキストを復旧できます. | | def foo(context) | $~ = context | printf "%s - %s\n", $&, $1 | end | "abc" =~ /a(..)/ | foo($~) なるほど、知りませんでした。 僕が思いついたのは、 def actions.call(str) each do |assoc| if str =~ assoc[0] args = [] i = 1 while match = eval("$#{i}") args << match i += 1 end assoc[1].call(*args) break end end end としておいて actions.add(/^(\S+)\s+(\S+)\s+(\S+)$/) do |first, second, third| printf "first = %s, second = %s, third = %s\n", first, second, third end とする、というものです。 変数名を付けられるのでわかりやすく書けそうな気がします。 # ところで$1,$2,...を格納している配列が入った変数ってあります? -- 前田 修吾