Can anyone offer a reasonable explanation for the difference in behavior
between the methods instance_variables and local_variables?

For example

  puts "before setting '@a'"
  puts "  instance variables = #{instance_variables.join(", ")}"
  puts
  @a = 20
  puts "after setting '@a'"
  puts " instance variables = #{instance_variables.join(", ")}"
  puts
  # >> before setting '@a'
  # >>   instance variables =
  # >>
  # >> after setting '@a'
  # >>  instance variables = @a
  # >>

instance_variables only lists '@a' _after_ it has been defined.

  puts "before setting 'a'"
  puts "  local variables = #{local_variables.join(", ")}"
  puts
  a = 10
  puts "after setting 'a'"
  puts "  local variables = #{local_variables.join(", ")}"
  puts
  # >> before setting 'a'
  # >>   local variables = a
  # >>
  # >> after setting 'a'
  # >>   local variables = a
  # >>

local_variables lists 'a' before it is defined.  It appears as though
local_variables is a list generated at parse time.

This caught me by surprise.  Their similar names implied for me similar
behavior.  Comments?
-- 
Posted via http://www.ruby-forum.com/.