Importing module to local namespace

I’m trying to do something like:

File: blah.rb

module Blah
def something
puts “something”
end
def another
puts “another”
end
end

File: blahtest.rb

require ‘blah’
Blah.something #-> “something”
Blah.another #-> “another”
import_to_local Blah
something #-> “something”
another #-> “another”

Coming from a Python background, I’m looking to do the approximate
equivalent of “from Blah import *”

File: blah.rb

module Blah
def something
puts “something”
end
def another
puts “another”
end
end

I meant:

File: blah.rb

module Blah
def Blah.something
puts “something”
end
def Blah.another
puts “another”
end
end

For the easiest thing, just stick with your first example of using
instance methods, and then “include Blah”. Why it works and what is
doing probably won’t really apparent, though.

You want to look into Mixins (and the keywords extend and include),
but you also want to get a good understanding of what local (self)
means in this context, because that will make which incantation you
want much easier to figure out, and rarely are you going to want to do
a bare include Foo or extend Foo on the top level context like that.

You should read the pickaxe as a bare minimum. The section on Modules
is a start: http://www.rubycentral.com/pickaxe/tut_modules.html Jay
Fields has a good intro, though, too that might answer your question
more directly:
http://blog.jayfields.com/2006/05/ruby-extend-and-include.html
Then you can start looking into the ClassMethods include idioms and
other fanciness.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs