文字列を変換したい:
"{john:123456}"
に:
"<script src='https://gist.github.com/john/123456.js'>"
動作するメソッドを書きましたが、非常にばかげています。次のようになります。
def convert
args = []
self.scan(/{([a-zA-Z0-9\-_]+):(\d+)}/) {|x| args << x}
args.each do |pair|
name = pair[0]
id = pair[1]
self.gsub!("{" + name + ":" + id + "}", "<script src='https://gist.github.com/#{name}/#{id}.js'></script>")
end
self
end
以下のようにこれを行う方法はありcool_method
ますか?
"{john:123}".cool_method(/{([a-zA-Z0-9\-_]+):(\d+)}/, "<script src='https://gist.github.com/$1/$2.js'></script>")