Importing module to local namespace

D

David --

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 *"
 
D

David --

#####
# 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
 
T

Tim Connor

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top