The explanation is a bit informal, though...

> #conceptual questions abound
> 
> class Bar
>  def show_hidden
>    x = "Yikes! Where did I come from? Is 'y' really an instance?"
>    puts x
>  end
> 
> puts "\n"
> puts "This is my 'self': #{self}\n"
> puts "Why does 'self' only print once?\nWhy not when I create the 'y'
> object?\n\n"
> 
> end  #class Bar

This is perfectly normal. While the ruby interpreter reads the 
code, it builds new classes, new methods and executes code. In
the code above, the interpreter sees, first of all, 'class Bar', meaning
a new class definition will be build. Second, it sees 'def show_hidden',
meaning a new (or a redefinition of a) method will be added to the class
Bar. Afterwards the interpreter sees some code that is not a
new class or method definition but some executable code. So it is
executed while reading the class definition.

> 
> class Foo
>  what_am_i = "If no instance of class Foo exists, why is this
printing?\n"
>  puts what_am_i
>  puts "what is the section of code outside any 'def' called?\n"
>  puts "If no instance of Foo exists, what is creating this Bar
instance?\n"
>  y = Bar.new
>  y.show_hidden
> 
> end  #class Foo

Same case. The class Foo has no new methods. You can check this out with:
  Bar.methods - Bar.superclass.methods   # --> []

Note: after the 'end #class Foo' line, the variable (actually it's a
reference) is out of scope.

> 
> 
> z = "\nJust can call me 'z'\n"
> puts z
> puts z.type
> puts "What about z? It is inside a class?\n\n"

No. Although you could say it is in the object self.

> 
> #  thanks

You're welcome!

> #  doug edmunds
> #  18 Mar 2001


Regards,
Paul.