From: gotoken@... (GOTO Kentaro) Date: 2000-10-15T04:34:41+09:00 Subject: [ruby-list:25456] What is universal %Q escape ごとけんです 拙作のxmpについて[ruby-talk:5437]からはじまるスレッドで言及 されてたので、忘れていたパッチ[ruby-talk:3755]を当てて更新し ました。本体は27行なのでこのメールの最後につけておきます。 http://www.ruby-lang.org/en/raa-list.rhtml?name=xmp そのパッチには問題があってその時は解決できなかったのですが、 ひとことでいうなら、プログラム中に絶対出て来ない文字は何かと 問題です(たぶん)。Aleksiはこの解答としてISO-8859-1の右側にあ る文字を使っていましたが、これは $KCODE != "NONE" のときはう まく行きません。そこで現在は、"\0" という文字はプログラムに は出て来ないだろうということで eval "%Q\0...\0" というような ことをしてます。すると、Aleksiから僕宛てに以下のようなコメン トが来ました。なにか御意見はございませんか? >Do you think you or someone else at ruby-list could compile some small set >of "rules" which to use in order to write code which is mostly suitable for >Japanese people without any modifications. This trick should definitely be >included as I use %Q notation all the time :). > >If you think you're busy or something, could you raise the issue at >ruby-list so that Western people would get gradually wiser ;]. -- gotoken module Kernel XMP_VERSION = "2000-10-12" XMP_DEFAULT_OPTION = ["%l\n #=> %r\n", "\n # "] def xmp(arg, fmt = XMP_DEFAULT_OPTION[0], sep=XMP_DEFAULT_OPTION[1]) if fmt fmt = fmt.to_s eval("___sep___ = #{sep.inspect}; ___res___ = []", TOPLEVEL_BINDING) eval(arg.gsub(/^(.*)\n?/){%Q| begin ___res___ << eval(%Q\0#{$1}\0).inspect.gsub(/^\\n/, ___sep___); rescue ___res___ << "!! " + $!; end|}, TOPLEVEL_BINDING) arg.split(/\n/).each_with_index{|l,i| (puts "\n" ; next) if l =~ /^$/ (puts l ; next) if l =~ /^\s*#/ res = fmt.gsub(/%l/, l) res = res.gsub(/%r/, eval("___res___", TOPLEVEL_BINDING)[i]) print res } else print arg; eval(arg, TOPLEVEL_BINDING) end end alias __xmp__ xmp end