[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Setting namespace in loaded files

Luke A. Kanies

8/22/2006 12:28:00 AM

Hi all,

I'm dynamically loading some ruby files, and those files need to call
a method on a module of mine. E.g., in this case, the method is
Puppet::Parser::Functions.newfunction, and the files are autoloaded
from 'puppet/parser/functions/<funcname>.rb'.

I'd like to load the files in a way that 'newfunction' could be
called without the full path to Puppet::Parser::Functions.; they call
'newfunction', and it correctly resolves to the Functions module.

Is there a way to load a file within an existing namespace, so that
the method search path started at the scope doing the loading?

Thanks,
Luke

--
Luke Kanies
http://m... | http://reducti... | 615-594-8199



9 Answers

khaines

8/22/2006 12:36:00 AM

0

e

8/22/2006 12:41:00 AM

0

unknown wrote:
> On Tue, 22 Aug 2006, Luke Kanies wrote:
>
>> Is there a way to load a file within an existing namespace, so that the
>> method search path started at the scope doing the loading?
>
> If you read the file, and then eval it in the desired scope, it has the
> effect of loading it into that scope.

There is some complexity/problem with this but
the details elude me. Came up when implementing
the require 'foo', MyNamespace thingy.

> Kirk Haines


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

Ara.T.Howard

8/22/2006 12:43:00 AM

0

e

8/22/2006 12:46:00 AM

0

Eero Saynatkari wrote:
> unknown wrote:
>> On Tue, 22 Aug 2006, Luke Kanies wrote:
>>
>>> Is there a way to load a file within an existing namespace, so that the
>>> method search path started at the scope doing the loading?
>>
>> If you read the file, and then eval it in the desired scope, it has the
>> effect of loading it into that scope.
>
> There is some complexity/problem with this but
> the details elude me. Came up when implementing
> the require 'foo', MyNamespace thingy.

Note to self, Enter does not do the same thing
as Ctrl-z.

The problem was that extending existing classes
etc. would cause issues since they would be also
enveloped in the new namespace. So this is only
an issue for general-purpose programming.

>> Kirk Haines


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

Luke A. Kanies

8/22/2006 6:51:00 PM

0

On Aug 21, 2006, at 7:36 PM, khaines@enigo.com wrote:

> On Tue, 22 Aug 2006, Luke Kanies wrote:
>
> If you read the file, and then eval it in the desired scope, it has
> the effect of loading it into that scope.

Hmmm. That complicates things a little, since I need to iterate
through $: myself, but it seems like about the only way.

Thanks.

--
Luke Kanies
http://m... | http://reducti... | 615-594-8199



Luke A. Kanies

8/22/2006 7:03:00 PM

0

On Aug 21, 2006, at 7:42 PM, ara.t.howard@noaa.gov wrote:
>
> food for thought:
>
> harp:~ > cat a.rb
> def context_load path, &context
> o = Object.new
> o.instance_eval &context
> o.instance_eval IO.read(path)
> end
>
> context_load 'b.rb' do
> def m() p 'foo' end
> end
>
> context_load 'b.rb' do
> def m() p 'bar' end
> end
> harp:~ > cat b.rb
> m()
>
>
> harp:~ > ruby a.rb
> "foo"
> "bar"

Yeah, if I decide to go this route (rather than just taking the easy,
already-working route of requiring a receiver for the method), it
definitely makes sense to pull it into a separate method.

> related
>
> http://codeforpeople.com/lib/ruby...

I'll look into that more closely. I've already used some of the
ideas in it, but I can't seem to understand the general idea behind
it. What problems are you solving with this?

Most of my dynamic loading is based on the assumption that I'm
loading someone else's code, so I want to do as much as possible for
the user. I don't want them to have to know how I'm automatically
loading their files, for instance.

I think for now, I'm just going to leave it as is; people can specify
the appropriate module as the receiver.

--
Luke Kanies
http://m... | http://reducti... | 615-594-8199



khaines

8/22/2006 7:04:00 PM

0

Ara.T.Howard

8/22/2006 7:25:00 PM

0

Ara.T.Howard

8/22/2006 8:25:00 PM

0