Re: Followup question on defining methods dynamically



Do modules not execute their own code (such as the 'p "Testing"') below in their own namespace, then? I'm doing something that's not at all object-oriented, and simply want to ensure that a bunch of methods don't leak out into the global namespace. One module will define those methods, and other modules (not classes, unless I really have to hack it that way) will use the first module, eg.

module Test
p "Testing"
eval("def foo\np 'OK'\nend")
end

module Run
include Test
foo
end

The above doesn't work, for the reasons you gave, but I still don't understand the semantics of why not, I'm afraid. No doubt my Python way of thinking. In Python, the equivalent code would create two modules, Test and Run, which are effectively just hashmaps mapping names to objects. The eval would define a 'foo' object in Test, so there would exist a function Test.foo. The include statement in Run (equivalent to Python's "from Test import *" I believe) would cause all names in Test to be imported into Run, resulting in a 'foo' entry in the Run hashmap; this would be Run.foo. Finally, code run within a module always has access to the module's namespace (hashmap) for variable lookup, so the call on 'foo' within Run would find 'Run.foo' and execute it.

Now, since that interpretation is obviously incorrect in Ruby, could someone provide details as to what is actually going on? If possible, I prefer explanations of the 'behind-the-scenes' stuff--how variables are looked up, namespaces are defined, etc., as understanding the background mechanisms will then make everything else pretty easy.

Thanks,
Ken


dblack@xxxxxxxxxxx wrote:
Hi --

On Sat, 4 Aug 2007, Kenneth McDonald wrote:

Simple example that doesn't work:

----------------
module Test
p "Testing"
eval("def foo\np 'OK'\nend")
foo
end
----------------

'foo' does not get defined in a place where it can be found by the call on foo, and I get an error. However, when I execute the two lines

eval("def foo\np 'OK'\nend")
foo

in IRB, everything works. I'm guessing that this is a namespace problem, but can't see why it would be. Then again, I'm accustomed to the Python namespace model, and still don't understand all of the ways in which the Ruby model is different.

Your first example is essentially doing this:

module Test
def foo
p "OK"
end

foo
end

When you define an instance method inside a module, you're defining it
for the future use of objects with that module in their ancestry:

class C
include Test
end

C.new.foo # OK


David



.



Relevant Pages

  • Re: macros
    ... (:method ((foo foo) ... (baz baz)) ... I don't see any namespace related anything in that. ... manually managed dispatch tables and plenty of other migraine-inducers ...
    (comp.lang.lisp)
  • Re: pre-PEP: Suite-Based Keywords - syntax proposal
    ... > def foo(): ... suite supplies a transient namespace where foo happens to ... Bengt Richter ...
    (comp.lang.python)
  • Re: Instance of inherited nested class in outer class not allowed?
    ... foo = Foo ... class statements are *executable* statements, not declarations. ... If it contains a class statement, it is executed as follows: create an empty namespace, execute the class body in it, create a new class object with __dict__ = that namespace, and finally, bind the class name to the newly created class object in the module namespace. ...
    (comp.lang.python)
  • Re: Class problem
    ... The Assembly Class (part of the System.Reflection namespace) is not ... and the 'URI' is a class definition. ... >>I added that code to the testing classes of foo and foo1. ... >> an example use the Object Browser to goto system.System and you will see ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: function conversion
    ... programmer, instead of, as is now usually done, to the library ... import and rename the namespace. ... namespace foo { ... without undermining the basic character of the language. ...
    (comp.lang.c)