我正在尝试使用 for 循环和 if 条件来使用File.open和puts函数创建文件。我的代码是
I want to write these entries only if it is not null. How to do it?
Edit: Full code is
需要'fileutils'需要'json'需要'open-uri'需要'pp'
数据 = JSON.parse('数据')
数组 = 数据如果数组 &.any?
Drafts_dir = File.expand_path('../drats', dir ) FileUtils.mkdir_p(drafts_dir)
array.each 做 |entry|
File.open(File.join(drafts_dir, "#{entry['twitter']}.md"), 'wb') do |draft|
keys = 1.upto(6).map { |i| "key_#{i}" }
values = keys.map { |k| "<img src='#{entry['image']} alt='image'>" if entry['image']}
# you can also do values = entry.values_at(*keys)
str = values.reject do |val|
val.nil? || val.length == 0
end.join("\n")
draft.puts str
end
结束结束
I need the the file `mark.md` as
https://somesite.com/image.png' alt='image'>
https://twitter.com/mark'>mark
and `kevin.md` likewise.