使用Ruby,我如何能够在写入输出文件的某些变量中自动转义单引号和双引号.来自PHP我正在寻找一个addlashes类型函数,但在Ruby中似乎没有一个简单的解决方案.
require "csv"
def generate_array( file )
File.open("#{file}" + "_output.txt", 'w') do |output|
CSV.foreach(file) do |img, _, part, focus, country, loc, lat, lon, desc, link|
output.puts("[#{lat}, #{lon}, '#{img.downcase}', '#{part}', '#{loc}', '#{focus}', '#{country}', '#{desc}', '#{link}'],")
end
end
end
ARGV.each do |file|
generate_array(file)
end
Run Code Online (Sandbox Code Playgroud)
我想你可以addslashes使用这个Ruby构造模拟PHP 功能:
.gsub(/['"\\\x0]/,'\\\\\0')
Run Code Online (Sandbox Code Playgroud)
例如:
slashed_line = %q{Here's a heavily \s\l\a\s\h\e\d "string"}
puts slashed_line.gsub(/['"\\\x0]/,'\\\\\0')
# Here\'s a heavily \\s\\l\\a\\s\\h\\e\\d \"string\"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2075 次 |
| 最近记录: |