Forum: Ruby Using 'include' but not poluting my namespace

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06db81f02537377c4bb207cdf8fd18ec?d=identicon&s=25 Mike Austin (Guest)
on 2006-03-10 20:55
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-10 21:13
(Received via mailing list)
On Sat, 11 Mar 2006, Mike Austin wrote:

> end
>
both pollute your namespace equally.  the first pollutes class Object,
the
second class DontPolute.  remember that you're code in ruby, if not
inside a
class, is inside the Object class.  this allows you to pretend your
coding
something like perl.  i always write main programs like so

   class Main
     ...
   end

   Main::new if $0 == __FILE__

to avoid any such issues.

if you are looking for a workaround you can also use

   Module::new{ include Inertia  ... }

regards.

-a
28ca4d5a4133c7c924a7ba464f5fc127?d=identicon&s=25 Brian Mattern (Guest)
on 2006-03-10 21:56
(Received via mailing list)
On Friday 10 March 2006 13:53, Mike Austin wrote:
> 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.
This topic is locked and can not be replied to.