[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Importing module to local namespace

David --

12/16/2007 6:39:00 AM

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 *"
--
Posted via http://www.ruby-....

2 Answers

David --

12/16/2007 6:46:00 AM

0

> #####
> # 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
--
Posted via http://www.ruby-....

Tim Connor

12/16/2007 7:40:00 AM

0

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_mo... Jay
Fields has a good intro, though, too that might answer your question
more directly: http://blog.jayfields.com/2006/05/ruby-extend-and-in...
Then you can start looking into the ClassMethods include idioms and
other fanciness.