The initial execution context of a Ruby program is an object, that is an instance of the Object class. If you attempt to query the object with inspect or p, it will simply return "main" as is mentioned here
Setting values such as @blah will create instance variables on this top level object.
The following short snippet should demonstrate this.
Inspecting the context simply returns main
, it's an Object
, more specifically an instance of theObject
class.
The initial list of instance variables is an empty array, but after you set the some using @blah, you can see they have been added to the list of instance variables.
In the example you posted, there is actually no need to use instance variables, local variables would have been fine.
p self
puts self.class
puts instance_of? Object
puts "instance variables = #{self.instance_variables}"
@db_host = "localhost"
@db_user = "root"
@db_pass = "root"
@db_name = "your_db_name"
puts "instance variables = #{self.instance_variables}"
main
Object
true
instance variables = []
instance variables = [:@db_host, :@db_user, :@db_pass, :@db_name]