Prev: Idiomatic Printing an array with commas
Next: User input with predefined and editable/completable entry
From: Rein Henrichs on 13 Jun 2010 23:30 On 2010-06-13 18:35:25 -0700, Rein Henrichs said: > Kernel#require does not namespace anything, no matter how you use it. > > Properly written Rubby libraries namespace their classes and modules. > If you own the code you're requiring, fix it. If not, find an > alternative to the code in question (which I find suspect based on this > lack of namespacing) or perhaps you may find some luck with Kernel#load. Also, properly written, it's "Ruby". Oops. -- Rein Henrichs http://puppetlabs.com http://reinh.com
From: Hagbard Celine on 14 Jun 2010 08:15 Thanks for your reply but I'm afraid that doesn't solve my problem. I stumbled upon the optional argument of `load' as well but it prevents me from accessing the loaded module. I'm sorry if I wasn't clear enough. I would need something like this: # foo.rb module Foo def method_a end def method_b end end # bar.rb def load_module load "foo.rb" puts Foo.methods end # `Foo' isn't known anymore If I use `load "foo.rb", true' I can't access the loaded module or am I mistaken? Security is a minor problem for the moment as it's just a hobby project. But I take a look at _why's sandbox anyway. Thanks for the hint. -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 14 Jun 2010 11:53 Hagbard Celine wrote: > Thanks for your reply but I'm afraid that doesn't solve my problem. I > stumbled upon the optional argument of `load' as well but it prevents me > from accessing the loaded module. I wish 'load' would simply return the anonymous module it has created, but there are nasty workarounds. For example: $ cat foo.rb module Foo def bar puts "hello" end module_function :bar end $res = Foo $ irb --simple-prompt >> load "foo.rb", true => true >> $res => #<Module:0xb741b218>::Foo >> $res.bar hello => nil -- Posted via http://www.ruby-forum.com/.
From: Joel VanderWerf on 14 Jun 2010 12:09 Hagbard Celine wrote: > Thanks for your reply but I'm afraid that doesn't solve my problem. I > stumbled upon the optional argument of `load' as well but it prevents me > from accessing the loaded module. I'm sorry if I wasn't clear enough. I > would need something like this: ... > If I use `load "foo.rb", true' I can't access the loaded module or am I > mistaken? That's correct (you can't access it without playing tricks like global vars or searching ObjectSpace). If security is not your concern, take a look at the script lib I mentioned before: http://redshift.sourceforge.net/script Unlike load(..., true) , Script.load returns the wrapper module.
From: Joel VanderWerf on 14 Jun 2010 12:12 Hagbard Celine wrote: > Thanks for your reply but I'm afraid that doesn't solve my problem. I > stumbled upon the optional argument of `load' as well but it prevents me > from accessing the loaded module. I'm sorry if I wasn't clear enough. I > would need something like this: > > # foo.rb > module Foo > def method_a > end > > def method_b > end > end > > # bar.rb > > def load_module > load "foo.rb" > puts Foo.methods > end > # `Foo' isn't known anymore Your example is not quite right. $ cat foo.rb module Foo def method_a end def method_b end end $ cat bar.rb def load_module load "foo.rb" puts Foo.instance_methods end load_module $ ruby bar.rb method_a method_b
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Idiomatic Printing an array with commas Next: User input with predefined and editable/completable entry |