2015-11-11 57 views
1

我有一個非常奇怪的問題。 如果我們運行這個類,由於binding.pry,執行將在方法調用中被中斷。Ruby初始化奇怪的參數性能

如果我們檢查receiver的方法值爲Ivo,如預期。但是`sender'的值就是這個對象,基本上就是params的數組。

["Gerard", 
"Ivo", 
#<Bank:0x007fb4339ac2b8 
    @accounts= 
    [#<Account:0x007fb4339ac240 @bank=#<Bank:0x007fb4339ac2b8 ...>, @founds=0, @user="Gerard">, 
    #<Account:0x007fb4339ac1f0 @bank=#<Bank:0x007fb4339ac2b8 ...>, @founds=0, @user="Ivo">]>, 
#<Bank:0x007fb4339ac2b8 
    @accounts= 
    [#<Account:0x007fb4339ac240 @bank=#<Bank:0x007fb4339ac2b8 ...>, @founds=0, @user="Gerard">, 
    #<Account:0x007fb4339ac1f0 @bank=#<Bank:0x007fb4339ac2b8 ...>, @founds=0, @user="Ivo">]>, 
1200] 

這是全班同學。任何解釋?

require_relative './lib/bank' 

class TransferAgent 
    def initialize(sender, receiver, sender_bank, receiver_bank, amount) 
    @sender  = sender, 
    @receiver  = receiver, 
    @sender_bank = sender_bank, 
    @receiver_bank = receiver_bank, 
    @amount  = amount 
    end 

    def call 
    binding.pry 
    transfer_result = perform_transfer until transfer_result.succed? 
    end 

    private 

    attr_reader :sender, :receiver, :amount, :sender_bank, :receiver_bank 

    def perform_transfer 
    sender_account.transfer_money(receiver_account, amount) 
    end 

    def sender_account 
    @sender_account ||= sender_bank.account sender 
    end 

    def receiver_account 
    @receiver_account ||= receiver_bank.account receiver 
    end 
end 

bank_a = Bank.new 

bank_a.create_account("Gerard") 
bank_a.create_account("Ivo") 

TransferAgent.new("Gerard", "Ivo", bank_a, bank_a, 1200).call 

回答

2

在您的初始化程序中,擺脫尾隨的逗號。整個初始化器被分解爲一個單一的並行分配。

這是怎麼回事的快速演示:

foo = 1,2,3 
foo 
=> [1,2,3] 

foo = 1, 
2, 
3 
foo 
=> [1,2,3]