Hi --

On Sat, 4 Oct 2008, Nit Khair wrote:

> Ragav Satish wrote:
>>>
>>> <code>
>>>  require 'common'
>>>  include 'Common'
>>>  class Prog < Application
>>>     f = Obj.new
>>>     g = Foo.new
>>>     #... etc ...
>>> </code>
>>>
>> Depends on where the module is included. If its included at the top
>> level (outside of another class/module) then it's mixed into the Object
>> class from which all other classes ultimately derive , so method lookup
>> on any object will always find it.
>> --Cheers
>> --Ragav
>
> Wow ! That said, earlier in the case i gave above is it at the top
> level?
>
> Because my situation was like this (before refactoring). There were 2
> classes in my file, and the first did NOT find it. It had to use a
> reference of the second class to call the module methods. Let me
> explain:
>
> ==== common.rb contains:
> def printme
> end
>
> ==== prog.rb
>
>  require 'common'
>  include 'Common'

That's an error: you include modules, not strings. But there's no
Common module anyway, so it's an error either way.

>  class Datasource
>      # saves reference to Prog in constructor
>      def initialize (app)
>         @main = app
>      end
>
>
>      printme         # this failed.
>      @main.printme   # this works

If you fix the errors I pointed out above, both of these call printme.
@main is nil at this point (and keep in mind that this @main has
nothing to do with the @main in initialize).

>    end
>
>   class Prog < Application
>      new Datasource(self)

No such method.

>      printme  # works
>   end

This is so full of errors that it's impossible to know what your real
problem was. Can you post some actual code that illustrates the
problem? It's not only easier for people trying to help you but
ultimately much more relevant for you, if you present your actual
problem and not a set of problems that only exist because you've
introduced them into your post.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!