On Behalf Of Bernd:
# class ABC
#   attr_accesor :first, :second

oops, watch the spell :)

# end
# 
# when I wrote the following:
# 
# x = ABC.new
# x.first = "test"
# puts x.instance_variables
# 
# the output was only
# @first

sure
 
# Is there a possibility to get also the unassigned variables? 

ruby is interpreted and dynamic and, in ruby, everything is an object, ergo, any object must come to life first. Variables only exist if you assign them something, or if they refer to something, it's where they get their life. We may go the backdoor route by inference..

irb(main):048:0> a=x.public_methods(false).select{|e| e["="]}.map{|e| "@"+e.chop}
=> ["@second", "@first"]
irb(main):049:0> b=x.instance_variables
=> ["@first"]
irb(main):050:0> a-b
=> ["@second"]   

thus @second is not yet vivified ..

but i am not sure if i am being rubyish here :(


# I mean, they exist, but they are only nil.

careful.   there is "exist" vs nil issue here. Eg, a method may return nil.  one may confuse methods with vars.

irb(main):005:0* class ABC
irb(main):006:1>   attr_accessor :first, :second
irb(main):007:1> end
=> nil
irb(main):009:0> x=ABC.new
=> #<ABC:0xb7d51990>
irb(main):010:0> x.first
=> nil
irb(main):015:0> x.instance_variables
=> []
irb(main):016:0> x.first=nil
=> nil
irb(main):017:0> x.first
=> nil
irb(main):018:0> x.instance_variables
=> ["@first"]

at this point @first now exist, but @second is not; ie, from the point of view of instance x of class ABC.