Add tags

Add tags to the following comment

変数v0を全てv1に置換した後に, 文字列とヒアドキュメント内のv1をv0に戻す処理をしています.

テストに使用したファイルを生成するコードも添付します.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def subst_var(file, v0, v1)
  hdocr = '[-]?[\"\'`]?([A-Z]+)'
  shared_reg = 's,;&|=+-*/[](){}'
  former, latter = %w[@$^ .].map{|reg|
    "#{shared_reg}#{reg}".each_char.inject(''){|a,b| "#{a}\\#{b}"}}
  gsub, rev_gsub = [:to_a, :reverse].map{|meth|
    proc{|s| s.gsub(*[v0, v1].__send__(meth))}}
  open(file, 'r'){|f| f.read}.
    gsub(/(?:^#{v0})?(?:[#{former}]#{v0})+[#{latter}]/, &gsub).
    gsub(/".*"/, &rev_gsub).
    gsub(/<<#{hdocr}(?:.|\s)+[^<]#{hdocr}/, &rev_gsub)
end
file = 'sample.rb'
puts subst_var(file, 'v0', 'v1')

open(file, 'w'){|f| f.write(<<EOF)}
v0+v0=v0
@v0 = 0
$v00, ^v0
"v0 v0 "
<<EOS
v0 v0
EOS
$v0, v0, v0
v0/v0*v0-v0+v0
EOF

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...