Ruby Tip: Instances

So I was working on a Rails project, helping someone upgrade from Rails 2 on Ruby 1.8.7 to Rails 3 on Ruby 1.9. There have been a few changes in the APIs. At one point, I was getting some exceptions. I needed to see what variables were available to an object. There were ifs and mixins and supers, so it was not always clear what I could call in a “puts” statement and what I could not.

So here is how you print out a list of the instance variables, and get a value:

puts "@article.instance_variable_names: #{@article.instance_variable_names.sort}"
puts "@article.instance_variable_get(@title): #{@article.instance_variable_get("@title")}"


There are 111 words in this article.