[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using 'include' but not poluting my namespace

Clint

3/10/2006 7:49:00 PM

I noticed that if you 'include' a module within a method, it will polute the
namesapce that defines that method. I found a simple solution is to declare a
throw-away class, and execute my code in that. Is there a more better way to
achieve this?

# Pollutes current namespace
def init()
include Inertia

do_stuff_with_inertia()
end

# Doesn't polute namespace, but ugly
class DontPolute
include Inertia

do_stuff_with_inertia()
end


Mike
2 Answers

Ara.T.Howard

3/10/2006 8:11:00 PM

0

Brian Mattern

3/10/2006 8:56:00 PM

0

On Friday 10 March 2006 13:53, Mike Austin wrote:
> I noticed that if you 'include' a module within a method, it will polute
> the namesapce that defines that method. I found a simple solution is to
> declare a throw-away class, and execute my code in that. Is there a more
> better way to achieve this?
>
> # Pollutes current namespace
> def init()
> include Inertia
>
> do_stuff_with_inertia()
> end
>
> # Doesn't polute namespace, but ugly
> class DontPolute
> include Inertia
>
> do_stuff_with_inertia()
> end
>
>
> Mike

Instead of include, you may want:
require 'intertia'

This will load up inertia.rb, which i'm assuming contains either a class or a
module name Intertia. Then you can use it via Interia.new() or
Intertia::do_stuff_with_intertia().

Correct me if I'm misunderstanding what you're trying to do here.
--
Brian Mattern