2013-05-15 2 views
0

저는 Cinch IRC 프레임 워크를 사용하여 사용자가 CRUD 따옴표를 허용하는 따옴표 함수를 만듭니다.다차원 해시 값 변경

문제가 있습니다. 필자는 YAML을 사용하여 따옴표를 저장하는 cinch-quote gem에 내 반복을 기반으로했습니다.

YAML에서 따옴표를 다차원 해시로로드합니다. delete_quote 메소드는 표시된 인용 부호를 ID로 찾아서 YAML 데이터베이스에서 '삭제됨'으로 표시해야합니다. 문제는이 YAML DB에서 삭제 된 값을 false에서 true로 변경하는 것입니다. 나는 루비에 익숙하지 않고이 코드는 아마 절대적으로 끔찍하고 우스꽝 스럽습니다. 잔인하시기 바랍니다.

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

왜 파일을 한 번 더 조회 :

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 
+0

YAML.load_file (@quotes_file) # 새로운 get_quotes 메서드를 사용하십시오. –

+0

파일을로드 한 후 YAML 및 이에 상응하는 Ruby 객체의 작은 샘플을 보여 주면 많은 도움이됩니다. –

답변

0

당신은 이미 사용하여 견적을 찾았나요?

대신, 단지 삭제로 표시 :

quote['deleted'] = true 

그런 다음 YAML에 따옴표를 덤프하고 파일을 저장합니다.

+0

나는 누군가가 쉽게 보일 것이라고 알고있었습니다. 감사! –