[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

module scope not clear to me

Nit Khair

10/4/2008 6:13:00 AM

I had a program which inherits from a superclass. It also has
common/convenience methods in a module.

<code>
require 'common'
include 'Common'
class Prog < Application
f = Obj.new
g = Foo.new
#... etc ...
</code>


Some convenience methods rely on a variable being set in the superclass.
(I know that does not sound good but it shields this detail from
individual programs.)

Then i refactored ... moved a lot of functionality out from the program
to objects it was creating. I would guess that these objects should
_not_ be able to call the module methods. Suprisingly, they were.

The problem came up that the variable was obviously not set when called
by these objects, (since the class of the module was now the object's
class not the program's class.)

So my basic question is: if a program includes a module, can all its
composed objects also call on the module.

What are the scope rules?

Any do's and don'ts/insights/suggestions are welcome.
--
Posted via http://www.ruby-....

5 Answers

ragav

10/4/2008 8:11:00 AM

0

Nit Khair wrote:
> I had a program which inherits from a superclass. It also has
> common/convenience methods in a module.
>
> <code>
> require 'common'
> include 'Common'
> class Prog < Application
> f = Obj.new
> g = Foo.new
> #... etc ...
> </code>
>
>
> Some convenience methods rely on a variable being set in the superclass.
> (I know that does not sound good but it shields this detail from
> individual programs.)
>
> Then i refactored ... moved a lot of functionality out from the program
> to objects it was creating. I would guess that these objects should
> _not_ be able to call the module methods. Suprisingly, they were.
>
> The problem came up that the variable was obviously not set when called
> by these objects, (since the class of the module was now the object's
> class not the program's class.)
>
> So my basic question is: if a program includes a module, can all its
> composed objects also call on the module.

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.

module M
def foo; puts "module foo" end
end

class C; end
# method name resolution chain before include
p C.ancestors #=> [C, Object, Kernel]

include M # including at top level has M as the

p C.ancestors #=> [C, Object, M, Kernel]

C.new.foo #=> module foo

# heck you can even call it on "M", "C" since they are also objects
M.foo #=> module foo
C.foo #=> module foo.

Once you include a module at the *top level* you are creating a set of
methods accessible from anywhere.

--Cheers
--Ragav
--
Posted via http://www.ruby-....

Nit Khair

10/4/2008 9:22:00 AM

0

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'

class Datasource
# saves reference to Prog in constructor
def initialize (app)
@main = app
end


printme # this failed.
@main.printme # this works
end

class Prog < Application
new Datasource(self)
printme # works
end

if __FILE__ == $0
p = new Prog...
p.run ...
end

== end of file

Here, class Prog was able to refer to all Module methods, but class
datasource could not and i had to pass a ref of Prog, so it could.
--
Posted via http://www.ruby-....

Nit Khair

10/4/2008 9:25:00 AM

0

> p = new Prog...

Sorry that should read Prog.new (old java habits still lingering) , the
above is not the real source code, just a represntative sample.
--
Posted via http://www.ruby-....

David A. Black

10/4/2008 11:33:00 AM

0

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.r... for details and updates!

Nit Khair

10/4/2008 12:29:00 PM

0

David A. Black wrote:
> Hi --
>
> On Sat, 4 Oct 2008, Nit Khair wrote:
>
>
> 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
My sincere apologies. In future I will make a running sample before
posting.

I did make a sample, and found that the problem was the *variable* that
was in the scope of one class, not the other. So my question is
basically wrong - sorry again for wasting your time.

--
Posted via http://www.ruby-....