2011-05-06 31 views
2

爲什麼下面的代碼導致錯誤「未定義局部變量或方法`foo_client」爲富::人物:類創建一個類的方法使用Ruby的問題

class Foo::People 

    class << self 
    def get_account_balance(account_num) 
     foo_client.request :get_account_balance, :body => {"AccountNum" => account_num} 
    end 
    end 

    def foo_client 
    @@client ||= Savon::Client.new do|wsdl, http| 
     wsdl.document = PEOPLE_SERVICE_ENDPOINT[:uri] + "?WSDL" 
     wsdl.endpoint = PEOPLE_SERVICE_ENDPOINT[:uri] 
    end 
    end 

end 

回答

7

def get_account_balanceclass << self塊內,所以這是一種類方法。 def foo_client不是,所以它是一個實例方法。所以你不能從get_account_balance調用foo_client,因爲你不是在People的實例上調用它。