Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access instance variable from outside the class

If an instance variable belongs to a class, can I access the instance variable (e.g. @hello) directly using the class instance?

class Hello   def method1     @hello = "pavan"   end end  h = Hello.new puts h.method1 
like image 596
Pawan Avatar asked Aug 25 '12 14:08

Pawan


People also ask

Can we access instance variable outside class?

The public instance variables are visible outside the class, and they can be accessed through a reference to the object to which they belong, by means of the field selection operator ``.

How do you access instance variables?

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference. VariableName.

How do you access instance variables in Ruby?

The instance variables of an object can only be accessed by the instance methods of that object. The ruby instance variables do not need a declaration. This implies a flexible object structure. Every instance variable is dynamically appended to an object when it is first referenced.

Can a class method access instance variables?

Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


2 Answers

Yes, you can use instance_variable_get like this:

class Hello   def method1     @hello = "pavan"   end end  h = Hello.new p h.instance_variable_get(:@hello) #nil p h.method1                        #"pavan" - initialization of @hello p 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   end  h = Hello.new p h.hello #nil p h.method1                        #"pavan" - initialization of @hello p 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.

like image 111
knut Avatar answered Oct 12 '22 23:10

knut


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

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

or

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

Calling attr_reader will create a getter for the given variable:

h = Hello.new p h.hello        #"pavan" 

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

h = Hello.new p 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

like image 33
Kevinvhengst Avatar answered Oct 12 '22 23:10

Kevinvhengst