简体   繁体   English

创建带有Ruby问题的类方法

[英]Creating a class method with Ruby problems

Why does the following code result in the error 'undefined local variable or method `foo_client' for Foo::People:Class' 为什么以下代码导致Foo :: People:Class的错误'undefined local variable or method`foo_client'

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

def get_account_balance is inside the class << self block, so it's a class method. def get_account_balanceclass << self块内部,因此它是一个类方法。 def foo_client is not, so it's an instance method. def foo_client不是,所以它是一个实例方法。 So you can't call foo_client from get_account_balance because you're not calling it on an instance of People . 因此,您不能从get_account_balance调用foo_client ,因为您没有在People实例上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM