ruby - Changing value in multidimensional hash -
i'm using cinch irc framework make quote function allows users crud quotes.
i'm running problem; based iteration on cinch-quote gem, uses yaml store quotes.
it loads quotes yaml multidimensional hash. delete_quote method needs find marked quote id, , mark 'deleted' in yaml database. problem i'm having changing deleted value false true in yaml db. i'm new ruby , code absolutely awful , laughable, please brutal.
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 master quote hash p quotes #returns hash i'm trying change. p quote if quote.nil? message_type(m, "quote id #{search} not exist.") else #again, master hash output = yaml.load_file(@quotes_file) #here's error. can't convert hash integer. can't figure out why # it'd generating that, or how fix it. mark_delete = output[quote]["deleted"] = "true" message_type(m, "quote #{search} deleted") end end end
you found quote using:
quote = quotes.find { |q| q["id"] == search.to_i }
why query file 1 more time?
instead, mark deleted:
quote['deleted'] = true
then dump quotes yaml , save file.
Comments
Post a Comment