0

我正在使用 Cinch IRC 框架制作一个报价功能,允许用户对报价进行 CRUD。

我遇到了问题;我的迭代基于 cinch-quote gem,它使用 YAML 来存储引号。

它将 YAML 中的引号加载到多维散列中。delete_quote 方法需要通过 ID 找到标记的报价,并在 YAML 数据库中将其标记为“已删除”。我遇到的问题是在此 YAML 数据库中将已删除的值从 false 更改为 true。我是 ruby​​ 新手,这段代码可能非常糟糕和可笑,请残酷。

  def get_quotes
    output = File.new(@quotes_file, 'r')
    quotes = YAML.load(output.read)
    output.close
    quotes
  end

  def delete_quote(m, search = nil)
    if is_admin?(m) && search.to_i > 0
      quotes = get_quotes
      quote = quotes.find { |q| q["id"] == search.to_i }
      #debugging stuff
      #returns the master quote hash
      p quotes
      #returns the hash that i'm trying to change.
      p quote
      if quote.nil?
        message_type(m, "Quote ID #{search} does not exist.")
      else
        #again, master hash
        output = YAML.load_file(@quotes_file)
        #here's the error. Can't convert Hash into Integer. I can't figure out why
        # it'd be generating that, or how to fix it.
        mark_delete = output[quote]["deleted"] = "true"
        message_type(m, "Quote #{search} was deleted")
      end

    end
  end
4

1 回答 1

0

您已经使用以下方法找到了报价:

quote = quotes.find { |q| q["id"] == search.to_i }

为什么要再查询一次文件?

相反,只需将其标记为已删除:

quote['deleted'] = true

然后将引号转储到 YAML 并保存文件。

于 2013-05-15T03:31:27.957 回答