On Wed, 11 Jul 2001, Sean Chittenden wrote:

> class Testing
>   @test_hash = Hash.new()
> 
>   public
>   def set(key, val)
>     @test_hash[key] = val
>   end
> end

That's a Java idiom that doesn't mean the same thing in Ruby.  In a class
definition, outside of a method definition, you are in the context of the
*class* object, not the instance.  So what you're doing there is defining
a class instance variable (sort of, but not really at all, like a static
variable in Java), whereas when you did it inside the initialize method
you were defining an instance variable.

This is confusing.  The short answer is, do it in the initialize instead.

For a start at the long answer, try this:

class Testing

	@var = "class"
	
	def initialize
		@var = "instance"
	end

	def Testing.print
		puts @var
	end

	def print
		puts @var
	end
end

t = Testing.new
t.print
Testing.print