需要帮助优化`each`迭代

时间:2015-08-26 19:18:12

标签: ruby iteration

我有.row-center { text-align: center; } .col-center { display: inline-block; border: 1px solid blue; text-align: left; float: none; }

@acct_list

我试图在银行计划中显示客户#和帐号#。我希望我的显示器看起来像这样:

{"account_id"=>1, "customer_id"=>1, "balance"=>0.0, 0=>1, 1=>1, 2=>0.0}
{"account_id"=>2, "customer_id"=>1, "balance"=>1000.0, 0=>2, 1=>nil, 2=>1000.0}
{"account_id"=>3, "customer_id"=>2, "balance"=>9.0, 0=>3, 1=>2, 2=>9.0}
{"account_id"=>4, "customer_id"=>2, "balance"=>0.0, 0=>4, 1=>2, 2=>0.0}

每位客户只能使用客户拥有的帐户总数显示一次。

这就是我的代码:

Customer #1 Account #1  -----  Balance: $0.0

            Account #2  -----  Balance: $1000.0

Customer #2 Account #3  -----  Balance: $9.0

            Account #4  -----  Balance: $0.0

显示def self.account_info2(acct_list) account_id_array = [] acct_list.each do |a| print "\n" print 'Customer #' + a["customer_id"].to_s + ' Account #' + a["account_id"].to_s + " ----- " + "Balance: $" + a["balance"].to_s print "\n" account_id_array.push(a["account_id"]) end account_id_array end @acct_list = bank.account_list(@man_name, @man_pin) return account_info2(@acct_list)

的不必要重复项
Customer #

3 个答案:

答案 0 :(得分:1)

@acct_list = [
  {"account_id"=>1, "customer_id"=>1, "balance"=>0.0, 0=>1, 1=>1, 2=>0.0},
  {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0, 0=>2, 1=>nil, 2=>1000.0},
  {"account_id"=>3, "customer_id"=>2, "balance"=>9.0, 0=>3, 1=>2, 2=>9.0},
  {"account_id"=>4, "customer_id"=>2, "balance"=>0.0, 0=>4, 1=>2, 2=>0.0}
]

@acct_list.chunk{|h| h["customer_id"]}
.each do |customer, a| a
  .each.with_index do |h, i|
    puts \
      (("Customer ##{customer}" if i.zero?)).to_s.ljust(11) +
      "Account ##{h["account_id"]}  -----  Balance: $#{h["balance"]}",
      nil
  end
end

输出:

Customer #1 Account #1  -----  Balance: $0.0

            Account #2  -----  Balance: $1000.0

Customer #2 Account #3  -----  Balance: $9.0

            Account #4  -----  Balance: $0.0

答案 1 :(得分:1)

似乎group_by会帮助你。您有一堆帐户数据需要按customer_id分组,然后处理输出。

group_by返回一个散列,其中键是分组项(customer_id'),值是输入数组。您实际上并不需要密钥,因此请在结果上调用.values以使customer_accounts成为一个数组数组,其中每个数组元素包含与一个客户关联的每个帐户的哈希数组。像这样:

[[{"account_id"=>1, "customer_id"=>1, "balance"=>0.0}, {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0}], [{"account_id"=>3, "customer_id"=>2, "balance"=>9.0}, {"account_id"=>4, "customer_id"=>2, "balance"=>0.0}]]

然后迭代customer_accounts以获取每个客户的信息,并使用customer选项迭代每个with_index,以便您只能将Customer #分配给第一个和account_output所需的间距,将所有结果收集到account_list = [{"account_id"=>1, "customer_id"=>1, "balance"=>0.0}, {"account_id"=>2, "customer_id"=>1, "balance"=>1000.0}, {"account_id"=>3, "customer_id"=>2, "balance"=>9.0}, {"account_id"=>4, "customer_id"=>2, "balance"=>0.0}] def account_info(acct_list) account_output = "" customer_accounts = acct_list.group_by { |account| account['customer_id'] }.values customer_accounts.each do |customer| customer.each_with_index do |account, index| if index == 0 account_output += "Customer ##{account['customer_id']} Account ##{account['account_id']} ----- Balance: $#{account['balance']}\n" else account_output += " Account ##{account['account_id']} ----- Balance: $#{account['balance']}\n" end end end print account_output end account_info(account_list) 字符串中。

<input type="text" name="entries[]"> /* Input ARRAY */

<input type="text" name="username"> 

<textarea name="message[]">...</textarea> /* Textarea ARRAY */

答案 2 :(得分:1)

list = [
    {"account_id" => 1, "customer_id" => 1, "balance" => 0.0, 0 => 1, 1 => 1, 2 => 0.0},
    {"account_id" => 2, "customer_id" => 1, "balance" => 1000.0, 0 => 2, 1 => nil, 2 => 1000.0},
    {"account_id" => 3, "customer_id" => 2, "balance" => 9.0, 0 => 3, 1 => 2, 2 => 9.0},
    {"account_id" => 4, "customer_id" => 2, "balance" => 0.0, 0 => 4, 1 => 2, 2 => 0.0}
]

list.chunk{|h| h["customer_id"]}.each { |customer_id, customer_data|
    msg = "Customer # #{customer_id}"
    cust_id_filter = ' ' * (msg.size)
    customer_data.each { |h|
        print (msg ? msg : cust_id_filter) + " Account # #{h["account_id"]} ---- Balance: $ #{h["balance"]}\n\n"
        msg = nil if msg
    }
}

你应该输出:

Customer # 1 Account # 1 ---- Balance: $ 0.0

             Account # 2 ---- Balance: $ 1000.0

Customer # 2 Account # 3 ---- Balance: $ 9.0

             Account # 4 ---- Balance: $ 0.0