What does it mean to create an instance variable, say @foo in a file outside any class definition. Say there is a file, test.rb and the entire contents of the file are given below.
# test.rb
@foo = "bar"
puts @foo
It prints "bar", but is this an instance variable in some sort of wrapping class?
Testing using two files indicates that there is a main object that everything is wrapped inside. Is this understanding correct?
Contents of a.rb
@me = self
@a = "from-a"
Contents of b.rb
require "./a"
@b = "from-b"
puts @me == self # true (self refers to the same object)
puts self.class # Object
puts self.instance_variables # [@a, @b, @me]
Everything is an object in ruby. So you are actually in the
mainobject right now, which is an instance ofObject.In your file, if you put
puts self.class, you will see you are operating undermain, and the class beingObject.Even in irb, for example: