2016-06-28 73 views
2

我正在嘗試查找排除某些類別中的對象的數組中的所有負數。這是一個數組的示例。Enumerable#find_all異常Ruby

@transactions = 
    [{"amount"=>-20, "name"=>"DEPOSIT", "category"=>["BENEFITS"], "category_id"=>"21007000"}, 
    {"amount"=>-0.8, "name"=>"XFER", "category"=>["Transfer", "Credit"], "category_id"=>"2106381"}, 
    {"amount"=>-20, "name"=>"DEPOSIT", "category"=>["Transfer", "Deposit"], 
"category_id"=>"21007000"}, 
    {"amount"=>-1, "name"=>"XFER", "category"=>["Transfer", "Credit"], 
"category_id"=>"21005000"}, 
    {"amount"=>300.80, "name"=>"XFER", "category"=>"Food", "category_id"=>"2106381"}] 

到目前爲止,我有類似的東西,但語法是錯誤的,它不起作用。我甚至不確定我可以在find_all塊上使用「條件」。

items = @transactions.find_all ({ |t| t.fetch('amount') != t.fetch('amount').abs, :conditions => [ t.fetch('category_id') == '2106381' || t.fetch('category') == ["Benefits"] != ?, any? ]}) 

因此,找到所有對象的數量爲負數,並從該列表中排除具有以下類別ID或類別名稱的對象。

由於只爲負,不具有的優點名稱或類別ID「2106381」

+0

請說明您的意思是「它是不正確的」。 [編輯]將這些信息寫入您的問題。 –

+0

給我們一個數組樣本,然後輸出所需的數據。 –

+0

我看到一些問題。 'find_all'需要一個塊,而不是一個參數,所以你想'find_all {...}'不'findall(...)'。另外,你的開頭'''永遠不會關閉。你能夠運行這些代碼嗎?如果沒有,請嘗試發佈一個至少可以運行的版本,即使它沒有按照您的要求進行。 – lwassink

回答

4

你的問題不是特別清楚,但我懷疑這你想要做什麼:

@transactions.select do |t| 
    t["amount"] < 0 && 
    t["category_id"] != "2106381" && 
    t["category"] != "Benefits" 
end 

可替代地,這是一個比較聲明(但稍微低效率):

@transactions.select {|t| t["amount"] < 0 } 
    .reject {|t| t["category_id"] == "2106381" || t["category"] == "Benefits" } 
+2

對於某些人來說,如果沒有否定和括號,並且有兩個'&&而不是一個'&&'和一個'||':'t [「amount」] <0 && t [「category_id」]!=「2106381」&& t [「category」]!=「Benefits」'。你的#2可能會冒犯感性,因爲它需要兩次通過。 –

+0

謝謝!像魅力一樣工作! – SupremeA

+1

'@ transactions.select(&:negative?)'只適用於Ruby 2.3+,請參閱:https://github.com/ruby/ruby/blob/3a48e12/numeric.c#L4196-L4197 – SoAwesomeMan

0

我相信這將這樣的伎倆對象的輸出:

negative_transactions = @transactions.find_all do |trans| 
    trans["amount"] < 0 && (trans["category"] != ["BENEFITS"] && trans["category_id"] != "2106381") 
end 

puts negative_transactions 
+0

我相信'''應該是'&&'。 OP的倒數第二段很清楚。 –

+0

感謝您對Cary的支持 –

2

它仍然是一個有點不清楚什麼你究竟想在這裏完成,但根據我的理解,你想:

1)包括所有交易以負amount 2)排除所有與指定交易category_id 3)排除所有交易與specefied category

要做到這一點,你可以做到以下幾點:

@transactions.find_all do |t| 
    t['amount'] < 0 && 
    t['category_id'] != '2106381' && 
    t['category'] != ["BENEFITS"] 
end 
1

下面的解決方案爲"amount"提供了三種不同的方案,並將"categories"的格式設置爲downcase。

require 'pp' # require pretty_print 

@transactions = [ 
    {"amount"=>-20, "name"=>"DEPOSIT", "category"=> ["BENEFITS"],"category_id"=>"21007000"}, 
    {"amount"=>-0.8, "name"=>"XFER", "category"=>["Transfer", "Credit"], "category_id"=>"2106381"}, 
    {"amount"=>-20, "name"=>"DEPOSIT", "category"=>["Transfer", "Deposit"], "category_id"=>"21007000"}, 
    {"amount"=>-1, "name"=>"XFER", "category"=>["Transfer", "Credit"], "category_id"=>"21005000"}, 
    {"amount"=>300.80, "name"=>"XFER", "category"=>"Food", "category_id"=>"2106381"} 
] 

# Enumerable#find_all 
# see: http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-find_all 
items = @transactions.find_all do |t| 

    # 1) amount is less than zero 
    t.fetch('amount') < 0 && 

    # 2) amount is less than zero or zero 
    #t.fetch('amount') <= 0 && 

    # 3) Ruby 2.3 use core method: Numeric#negative? 
    # see: http://ruby-doc.org/core-2.3.0/Numeric.html#method-i-negative-3F 
    # see: https://github.com/ruby/ruby/blob/3a48e12/numeric.c#L4196-L4197 
    # via: http://stackoverflow.com/a/34146626 
    #t.fetch('amount').negative? && 

    t.fetch('category_id') != '2106381' && 

    # category is array so map and downcase before Array#include? 
    # see: http://ruby-doc.org/core-2.2.3/Array.html#method-i-map 
    # see: http://ruby-doc.org/core-2.2.3/Array.html#method-i-include-3F 
    !(t.fetch('category').map {|c| c.downcase}.include?("benefits")) 
end 

pp items 
# [{"amount"=>-20, 
# "name"=>"DEPOSIT", 
# "category"=>["Transfer", "Deposit"], 
# "category_id"=>"21007000"}, 
# {"amount"=>-1, 
# "name"=>"XFER", 
# "category"=>["Transfer", "Credit"], 
# "category_id"=>"21005000"}]