语法错误,意外的keyword_end,预期为')'

时间:2018-12-31 16:02:45

标签: json ruby

我有一个名为import.rb的脚本,它将json内容从url导入草稿目录。

require 'fileutils'
require 'json'

# Load JSON data from source
# (Assuming the data source is a json file on your file system)
data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec')

# Proceed to create post files if the value array is not empty
array = data["user"]
if array && !array.empty?
  # create the `_drafts` directory if it doesn't exist already
  drafts_dir = File.expand_path('./_drafts', __dir__)
  FileUtils.mkdir_p(posts_dir) unless Dir.exist?(drafts_dir)

  # iterate through the array and generate draft-files for each entry
  # where entry.first will be the "content" and entry.last the "title"
  array.each do |entry|
    File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
      draft.puts("---\n---\n\n#{entry.first}"
    end
  end
end

运行ruby _scripts/import.rb时出现_scripts/import.rb:20: syntax error, unexpected keyword_end, expecting ')' end错误。我将行从推荐形式更改为data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec')。原始为data = JSON.parse(File.read(your_json_source.json))。请帮助我。

1 个答案:

答案 0 :(得分:1)

问题是19行末尾缺少)

 ...

 array.each do |entry|
   File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
     draft.puts("---\n---\n\n#{entry.first}")
   end
 end

它应该解决该问题:)