Neil Middleton
Neil Middleton

Reputation: 22240

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'

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

Upvotes: 2

Views: 60

Answers (1)

sepp2k
sepp2k

Reputation: 370455

def get_account_balance is inside the class << self block, so it's a class method. def foo_client is not, so it's an instance method. So you can't call foo_client from get_account_balance because you're not calling it on an instance of People.

Upvotes: 7

Related Questions