Re: [MacRuby-devel] require [was: [ANN] The Flying Camera (game)]
Date: Fri, 17 Dec 2010 11:13:35 -0800 From: Gabriel Gilder <gabriel@gabrielgilder.com>
I ended up solving this problem in my MacRuby app by writing a little class to load dependencies - it keeps track of the full file paths of every dependency in a class variable and makes sure to only load a file once even if you request it again. Let me know if you would like me to post some sample code...
This is just reinventing Ruby's $" builtin variable - require stores the name of files you require in here and doesn't load them if they've been loaded already. If that's not working for you and you're getting errors with library files being loaded multiple times, you should check that $: (aka $LOAD_PATH) is set correctly and you're not requiring files with relative pathnames. The names stored in $" aren't normalised, they're exactly what you passed to require, so if you have a file hierarchy like main.rb lib/ | foo.rb | bar.rb \ baz/ \ asd.rb where main.rb includes require './lib/baz/asd' require './lib/bar' and asd.rb has require '../bar' then bar will be included twice since '../bar' does not match './lib/bar'. what you should do is add the lib directory to $: and then change the require lines in main.rb to require 'baz/asd' require 'bar' and in asd.rb to require 'bar' so that the normal mechanism that require uses to only load files once can actually work as designed. If for some reason this is impossible, you could also change all your require lines from require '../bar' to require File.expand_path('../bar') which should expand everything out to absolute paths so that (once again) the path matching in require can work as designed. cheers Russell
Thanks for the tip, Russell, I did not know about the $" variable. I'll have to revisit my code and see if I can eliminate some redundancy... currently my code is using File.expand_path to ensure I don't have two different paths to the same file, but maybe modifying $LOAD_PATH would be more elegant. -Gabriel On Sat, Dec 18, 2010 at 1:58 PM, russell muetzelfeldt < russm-macruby-devel@slofith.org> wrote:
Date: Fri, 17 Dec 2010 11:13:35 -0800 From: Gabriel Gilder <gabriel@gabrielgilder.com>
I ended up solving this problem in my MacRuby app by writing a little class to load dependencies - it keeps track of the full file paths of every dependency in a class variable and makes sure to only load a file once even if you request it again. Let me know if you would like me to post some sample code...
This is just reinventing Ruby's $" builtin variable - require stores the name of files you require in here and doesn't load them if they've been loaded already.
If that's not working for you and you're getting errors with library files being loaded multiple times, you should check that $: (aka $LOAD_PATH) is set correctly and you're not requiring files with relative pathnames. The names stored in $" aren't normalised, they're exactly what you passed to require, so if you have a file hierarchy like
main.rb lib/ | foo.rb | bar.rb \ baz/ \ asd.rb
where main.rb includes
require './lib/baz/asd' require './lib/bar'
and asd.rb has
require '../bar'
then bar will be included twice since '../bar' does not match './lib/bar'. what you should do is add the lib directory to $: and then change the require lines in main.rb to
require 'baz/asd' require 'bar'
and in asd.rb to
require 'bar'
so that the normal mechanism that require uses to only load files once can actually work as designed. If for some reason this is impossible, you could also change all your require lines from
require '../bar'
to
require File.expand_path('../bar')
which should expand everything out to absolute paths so that (once again) the path matching in require can work as designed.
cheers
Russell
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 2010-12-18, at 19:58 , russell muetzelfeldt wrote:
then bar will be included twice since '../bar' does not match './lib/bar'. what you should do is add the lib directory to $: and then change the require lines in main.rb to
I thought $" expanded the paths on its own. I'm stupidly lazy to re-check that, but I would go as far as saying I'm 90% sure. I guess it also doesn't hurt to point out the seldom-heard-of 1.9-ism require_relative.
It's also worth pointing out that require_relative is not yet implemented in MacRuby... On Mon, Dec 20, 2010 at 8:34 AM, Caio Chassot <lists@caiochassot.com> wrote:
On 2010-12-18, at 19:58 , russell muetzelfeldt wrote:
then bar will be included twice since '../bar' does not match
'./lib/bar'. what you should do is add the lib directory to $: and then change the require lines in main.rb to
I thought $" expanded the paths on its own. I'm stupidly lazy to re-check that, but I would go as far as saying I'm 90% sure.
I guess it also doesn't hurt to point out the seldom-heard-of 1.9-ism require_relative.
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
I don't believe paths are expanded normally by require, when they include a relative component like "../" at least. In a quick experiment I just whipped up, the following two lines only result in one load: require 'subdir/module' require './subdir/module' But these two lines result in the file being loaded twice: require 'subdir/module' require 'subdir/../subdir/module' require_relative sounds handy, hope that makes it into MacRuby at some point... -Gabriel On Mon, Dec 20, 2010 at 5:34 AM, Caio Chassot <lists@caiochassot.com> wrote:
On 2010-12-18, at 19:58 , russell muetzelfeldt wrote:
then bar will be included twice since '../bar' does not match
'./lib/bar'. what you should do is add the lib directory to $: and then change the require lines in main.rb to
I thought $" expanded the paths on its own. I'm stupidly lazy to re-check that, but I would go as far as saying I'm 90% sure.
I guess it also doesn't hurt to point out the seldom-heard-of 1.9-ism require_relative.
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (4)
-
Caio Chassot
-
Gabriel Gilder
-
Joshua Ballanco
-
russell muetzelfeldt