-1

I have a class where I create a new instance variable called @client which is an object in itself. Now, in another file I create an object from this class, but cannot access the methods of the @client object. How do I do?

example:

class A_Helper
    def initialize
        @client = Module::Client.new('info')
        p @client
        # <Module::Client:0x000000017fd018>
    end
end
---------------------------
class A_Controller
    @A_Helper = A_Helper.new
    p @A_Helper.client
    # <class:A_Controller>: undefined method `client' for nil:NilClass
Giygas
  • 11
  • 1

1 Answers1

1

you have declared a local variable @client, which his accessible only inside your class. If you want to get access from outside, you need to declare getter or shortcuts like attr_accessor/attr_reader (check this post ).

class A_Helper
  attr_reader :client

  def initialize
    @client = Module::Client.new('info')
  end
end
Community
  • 1
  • 1
Traveler
  • 191
  • 1
  • 10
  • It gets access to the object, but i still cant do A_helper.client.Method(), as it says "undefined method `client' for nil:NilClass" edit: sorry, didnt make it clear in the example im trying to call methods in client – Giygas Apr 09 '17 at 21:46