Access instance variable from outside the class Access instance variable from outside the class ruby ruby

Access instance variable from outside the class


Yes, you can use instance_variable_get like this:

class Hello  def method1    @hello = "pavan"  endendh = Hello.newp h.instance_variable_get(:@hello) #nilp h.method1                        #"pavan" - initialization of @hellop h.instance_variable_get(:@hello) #"pavan"

If the variable is undefined (first call of instance_variable_get in my example) you get nil.


As Andrew mention in his comment:

You should not make this the default way you access instance variables as it violates encapsulation.

A better way is to define an accessor:

class Hello  def method1    @hello = "pavan"  end  attr_reader :hello  endh = Hello.newp h.hello #nilp h.method1                        #"pavan" - initialization of @hellop h.hello #"pavan"

If you want another method name, you could alias the accessor: alias :my_hello :hello.

And if the class is not defined in your code, but in a gem: You can modify classes in your code and insert new functions to classes.


You can also accomplish this by calling attr_reader or attr_accessor like this:

class Hello  attr_reader :hello  def initialize    @hello = "pavan"  endend

or

class Hello  attr_accessor :hello  def initialize    @hello = "pavan"  endend

Calling attr_reader will create a getter for the given variable:

h = Hello.newp h.hello        #"pavan"

Calling attr_accessor will create a getter AND a setter for the given variable:

h = Hello.newp h.hello        #"pavan"h.hello = "John"p h.hello        #"John"

As you might understand, use attr_reader and attr_accessor accordingly. Only use attr_accessor when you need a getter AND a setter and use attr_reader when you only need a getter