6

I've a problem with the instance_variable_get method cause it's always returns nil object with one of my object instance. Here is my code:

logger.info "ASDF: " + @d_tree.inspect
logger.info "ASDF: " + @d_tree.instance_variable_get(:@content);

and the output is:

ASDF: #<DTree id: 11, parent_id: nil, content: "bababababa", subsidiary_info: "", deep_info: "blabla", title: "hello", direction: 1, created_at: "2010-10-26 19:27:32", updated_at: "2010-11-01 23:14:31", color: 2, cell_color: 2, howtoinfo: "howtoinfooo", textinfo: "textInfooo", locationinfo: "locationInfoooo", productinfo: "productinfoooo">
TypeError (can't convert nil into String):
    /app/controllers/d_trees_controller.rb:38:in `+'

According to the inspect the object seems to be fine, but the instance_variable_get returns a nil object

Thanks for your help!

seriakillaz
  • 833
  • 12
  • 22
  • Don't forget to turn your warnings on: see http://stackoverflow.com/questions/3955688/how-do-i-debug-ruby-scripts for how to do so. – Andrew Grimm Nov 02 '10 at 22:29

3 Answers3

3

instance_variable_get(arg)

It should return the value of the instance variable or nil if the instance variable is not set.

for example

we define the following class

  class Velpradeep
    def initialize(mark1, mark2)
      @m, @s = mark1, mark2
    end
end

During creation of the object of the class

obj = Velpradeep.new(98,96)

Then we can access the instance variables by using :

irb(main):046:0> obj.instance_variable_get(:@m)
=> 98

Access the undefined instance variables defined in the initialize method

irb(main):047:0> obj.instance_variable_get(:@p)
=> nil # provides nil bcz the instance variable is not defined 

If you want to access the variable before you need to set the instance variable using

instance_variable_set()

example :

irb(main):048:0> obj.instance_variable_set(:@p, 99)
=> 99

Then we can use, it will return the value of the instance variable....

irb(main):049:0> obj.instance_variable_get(:@p)
=> 99
vel pradeep.MS
  • 584
  • 5
  • 7
2

Although it's considered bad form to grab instance variables like this directly, as using attr_accessor is the preferred method, the problem in this particular instance is that there is no instance variable called @content. What you have appears to be an ActiveRecord attribute.

These are fetched using a different mechanism:

@d_tree.attributes[:content]

Generally this is even a little redundant as this will needlessly create a copy of the attributes hash. The typical way to access is:

@d_tree.content

These values are actually located in the @attributes instance variable managed by ActiveRecord.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

instance_variable_get returns nil if the variable is uninitialized (or if it has been set to nil, of course).

So, obviously, @content hasn't been initialized yet.

Why it hasn't been initialized is impossible to tell, since you haven't shown us any actual code yet.

It looks like @d_tree might be an ActiveRecord object, in which case the solution is rather simple: ActiveRecord objects don't keep their state in instance variables, they are backed by a database.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653