DaVinci:

>  I have a rpoblem with object orientation. Intention is to define
>  instances usable from all classes. For example:
> 
>  	class Configuration
> 		attr_reader :size
> 	
> 		def initialize(size)
> 			@size = size
> 		end
> 	end
> 
> 	class Foo
> 		def initialize
> 			puts @conf.size
> 		end
> 	end
> 
> 	attr_reader :conf
> 	@conf = Configuration.new(100)
> 	Foo.new
> 
> 	exit(0)
> 
>  Why I can not use attr_reader form base class?. Might I use a global
>  variable to define instance?.
> 
>  I need your ideas about "best way of doing it" :)

I guess what you're after, but I have to say I was a bit lost too.

My guess is that you need to use a global variable. It's is exactly a
variable referring to an instance and it's usable from all classes.

  class Foo
    def initialize
      puts $conf.size
    end
  end

  $conf = Configuration.new(100)

Let me remind you, that you very seldom (if ever) need global variables.
It's much better to use instance variables, or if they're too expensive
(performance, memory constraints) then class variables.

Let's take an example. You wrote nice component class called Foo, and Mr.
Zak wrote immensely useful library Zak, which uses $conf as a short hand for
$confine = true || false. Now I like to write a new program using Zak, and
your Foo, but unfortunately I can't as both use $conf and in different way.

There would be no problem if your library had

  class Foo
    @@config = Configuration.new
  end

instead of usage of $conf. Now I have to leave one lib out, or fix one or
both of them. If they're really good ones, fixing would probably be better,
but then I'm rewriting code which should have been nice from the beginning.

This is also a reason why you should never write

  require 'foo'
  str = gets.split

as you're not giving the split pattern, and $; is used instead. This
prohibition applies unless you know Foo really well, so that it won't change
the value of $; (even for convenience, as usual) and you won't install new
version of Foo without checking the source.

Usually people fool around, and trust on their luck. But if I don't have to,
I'd like to save my luck for harder cases. This one I can very simply cover
by myself.

	- Aleksi