Access instance variable from outside the class
Asked Answered
P

3

100

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
Procurable answered 25/8, 2012 at 14:15 Comment(0)
O
178

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.

Ophthalmia answered 25/8, 2012 at 14:19 Comment(6)
You should not make this the default way you access instance variables as it violates encapsulation.Photozincography
@Ophthalmia can't we just do this like h = Hello.new and h.method1 and h.hello ?Bunnybunow
@nlingutla You can define an accessor with attr_reader :helloOphthalmia
Wouldn't it be alias my_hello hello, not alias :my_hello :hello?Neomaneomah
@QPaysTaxes I learned alias needs symbols as parameter, so no, my version is correct and wanted as is. But I tested your code and it works also - I'm surprised about that. The documentations also use the symbols, e.g. ruby.about.com/od/rubyfeatures/a/aliasing.htmOphthalmia
For me, this is great for specs, where you don't want to make something public.Timpani
S
27

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

Sweettalk answered 23/7, 2016 at 17:46 Comment(0)
T
1

In case anyone wants to access the variable to set, here how they can do it:

puts h.instance_variable_get(:@hello) # nil
h.instance_variable_set(:@hello, "StackOverflow") # <- set the value
puts h.instance_variable_get(:@hello) # StackOverflow
Thurmanthurmann answered 8/5, 2023 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.