我试图找出数组中的所有负数,但不包括某些类别中的对象。下面是数组的一个示例。
@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“的好处。
发布于 2016-06-28 23:27:52
你的问题不是特别清楚,但我怀疑这能做你想做的事:
@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" }发布于 2016-06-28 23:26:09
现在还不清楚你到底想要实现什么,但根据我的理解,你想要:
( 1)包括所有负amount的事务2)排除具有指定category_id 3的所有事务)排除具有指定category的所有事务
要做到这一点,可以执行以下操作:
@transactions.find_all do |t|
t['amount'] < 0 &&
t['category_id'] != '2106381' &&
t['category'] != ["BENEFITS"]
end发布于 2016-06-29 00:17:39
下面的解决方案为"amount"提供了三种不同的场景,并将"categories"格式化为小写。
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"}]https://stackoverflow.com/questions/38087406
复制相似问题