Why does Ruby 1.9.2 remove “.” from LOAD_PATH, and what's the alternative?












154















The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all require statements that based off the project path). Was there a particular justification for doing this?



As for a fix, adding $: << "." everywhere works, but seems incredibly hacky and I don't want to do that. What's the preferred way to make my Rakefiles 1.9.2+ compatible?










share|improve this question





























    154















    The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all require statements that based off the project path). Was there a particular justification for doing this?



    As for a fix, adding $: << "." everywhere works, but seems incredibly hacky and I don't want to do that. What's the preferred way to make my Rakefiles 1.9.2+ compatible?










    share|improve this question



























      154












      154








      154


      66






      The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all require statements that based off the project path). Was there a particular justification for doing this?



      As for a fix, adding $: << "." everywhere works, but seems incredibly hacky and I don't want to do that. What's the preferred way to make my Rakefiles 1.9.2+ compatible?










      share|improve this question
















      The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all require statements that based off the project path). Was there a particular justification for doing this?



      As for a fix, adding $: << "." everywhere works, but seems incredibly hacky and I don't want to do that. What's the preferred way to make my Rakefiles 1.9.2+ compatible?







      ruby rake require load-path






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 25 '11 at 23:52









      Milele

      334




      334










      asked May 24 '10 at 21:12









      John FeminellaJohn Feminella

      239k36302328




      239k36302328
























          7 Answers
          7






          active

          oldest

          votes


















          140














          It was deemed a "security" risk.



          You can get around it by using absolute paths



          File.expand_path(__FILE__) et al


          or doing



          require './filename' (ironically).


          or by using



          require_relative 'filename'


          or adding an "include" directory



          ruby -I . ...


          or the same, using irb;



          $irb -I .





          share|improve this answer





















          • 26





            I wound up using require_relative. Thanks.

            – John Feminella
            May 24 '10 at 22:50






          • 11





            Is this akin to most unixes not including the current directory in the path for running executables?

            – Andrew Grimm
            May 24 '10 at 23:56






          • 1





            @Andrew yes I think so.

            – rogerdpack
            May 25 '11 at 3:32






          • 5





            require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

            – mxcl
            Aug 2 '12 at 18:31



















          34














          There's two reasons:




          • robustness and

          • security


          Both are based on the same underlying principle: in general, you simply cannot know what the current directory is, when your code is run. Which means that, when you require a file and depend on it being in the current directory, you have no way of controlling whether that file will even be there, or whether it is the file that you actually expect to be there.






          share|improve this answer



















          • 5





            I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

            – John Feminella
            May 25 '10 at 12:11






          • 4





            @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

            – Jörg W Mittag
            May 25 '10 at 12:53













          • So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

            – Joshua Cheek
            Sep 11 '10 at 22:07






          • 4





            @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

            – Jörg W Mittag
            Sep 11 '10 at 22:22








          • 8





            @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

            – Jörg W Mittag
            Sep 11 '10 at 22:24



















          16














          As others answers point out, it's a security risk because . in your load path refers to the present working directory Dir.pwd, not the directory of the current file being loaded. So whoever is executing your script can change this simply by cding to another directory. Not good!



          I've been using full paths constructed from __FILE__ as an alternative.



          require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))


          Unlike require_relative, this is backward compatible with Ruby 1.8.7.






          share|improve this answer



















          • 4





            There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

            – Tyler Rick
            May 6 '11 at 23:12





















          8














          Use require_relative 'file_to_require'



          Throw this in your code to make require_relative work in 1.8.7:



          unless Kernel.respond_to?(:require_relative)
          module Kernel
          def require_relative(path)
          require File.join(File.dirname(caller.first), path.to_str)
          end
          end
          end





          share|improve this answer

































            6














            '.' in your path has long been considered a bad thing in the Unix world (see, for example, http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html). I assume the Ruby folks have been persuaded of the wisdom of not doing that.






            share|improve this answer































              3














              I found this to be a confounding change until I realized a couple of things.



              You can set RUBYLIB in your .profile (Unix) and go on with life as you did before:



              export RUBYLIB="."



              But as mentioned above, it's long been considered unsafe to do so.



              For the vast majority of cases you can avoid problems by simply calling your Ruby scripts with a prepended '.' e.g. ./scripts/server.






              share|improve this answer































                3














                As Jörg W Mittag pointed out, I think what you want to be using is require_relative so the file you require is relative to the source file of the require declaration and not the current working dir.



                Your dependencies should be relative to your rake build file.






                share|improve this answer

























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2900370%2fwhy-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  140














                  It was deemed a "security" risk.



                  You can get around it by using absolute paths



                  File.expand_path(__FILE__) et al


                  or doing



                  require './filename' (ironically).


                  or by using



                  require_relative 'filename'


                  or adding an "include" directory



                  ruby -I . ...


                  or the same, using irb;



                  $irb -I .





                  share|improve this answer





















                  • 26





                    I wound up using require_relative. Thanks.

                    – John Feminella
                    May 24 '10 at 22:50






                  • 11





                    Is this akin to most unixes not including the current directory in the path for running executables?

                    – Andrew Grimm
                    May 24 '10 at 23:56






                  • 1





                    @Andrew yes I think so.

                    – rogerdpack
                    May 25 '11 at 3:32






                  • 5





                    require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                    – mxcl
                    Aug 2 '12 at 18:31
















                  140














                  It was deemed a "security" risk.



                  You can get around it by using absolute paths



                  File.expand_path(__FILE__) et al


                  or doing



                  require './filename' (ironically).


                  or by using



                  require_relative 'filename'


                  or adding an "include" directory



                  ruby -I . ...


                  or the same, using irb;



                  $irb -I .





                  share|improve this answer





















                  • 26





                    I wound up using require_relative. Thanks.

                    – John Feminella
                    May 24 '10 at 22:50






                  • 11





                    Is this akin to most unixes not including the current directory in the path for running executables?

                    – Andrew Grimm
                    May 24 '10 at 23:56






                  • 1





                    @Andrew yes I think so.

                    – rogerdpack
                    May 25 '11 at 3:32






                  • 5





                    require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                    – mxcl
                    Aug 2 '12 at 18:31














                  140












                  140








                  140







                  It was deemed a "security" risk.



                  You can get around it by using absolute paths



                  File.expand_path(__FILE__) et al


                  or doing



                  require './filename' (ironically).


                  or by using



                  require_relative 'filename'


                  or adding an "include" directory



                  ruby -I . ...


                  or the same, using irb;



                  $irb -I .





                  share|improve this answer















                  It was deemed a "security" risk.



                  You can get around it by using absolute paths



                  File.expand_path(__FILE__) et al


                  or doing



                  require './filename' (ironically).


                  or by using



                  require_relative 'filename'


                  or adding an "include" directory



                  ruby -I . ...


                  or the same, using irb;



                  $irb -I .






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 22 '17 at 18:35

























                  answered May 24 '10 at 21:44









                  rogerdpackrogerdpack

                  34.5k16133254




                  34.5k16133254








                  • 26





                    I wound up using require_relative. Thanks.

                    – John Feminella
                    May 24 '10 at 22:50






                  • 11





                    Is this akin to most unixes not including the current directory in the path for running executables?

                    – Andrew Grimm
                    May 24 '10 at 23:56






                  • 1





                    @Andrew yes I think so.

                    – rogerdpack
                    May 25 '11 at 3:32






                  • 5





                    require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                    – mxcl
                    Aug 2 '12 at 18:31














                  • 26





                    I wound up using require_relative. Thanks.

                    – John Feminella
                    May 24 '10 at 22:50






                  • 11





                    Is this akin to most unixes not including the current directory in the path for running executables?

                    – Andrew Grimm
                    May 24 '10 at 23:56






                  • 1





                    @Andrew yes I think so.

                    – rogerdpack
                    May 25 '11 at 3:32






                  • 5





                    require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                    – mxcl
                    Aug 2 '12 at 18:31








                  26




                  26





                  I wound up using require_relative. Thanks.

                  – John Feminella
                  May 24 '10 at 22:50





                  I wound up using require_relative. Thanks.

                  – John Feminella
                  May 24 '10 at 22:50




                  11




                  11





                  Is this akin to most unixes not including the current directory in the path for running executables?

                  – Andrew Grimm
                  May 24 '10 at 23:56





                  Is this akin to most unixes not including the current directory in the path for running executables?

                  – Andrew Grimm
                  May 24 '10 at 23:56




                  1




                  1





                  @Andrew yes I think so.

                  – rogerdpack
                  May 25 '11 at 3:32





                  @Andrew yes I think so.

                  – rogerdpack
                  May 25 '11 at 3:32




                  5




                  5





                  require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                  – mxcl
                  Aug 2 '12 at 18:31





                  require './filename' only works if your script is executed with the working directory set to the same directory that the script resides. This is often not the case in multi-directory projects.

                  – mxcl
                  Aug 2 '12 at 18:31













                  34














                  There's two reasons:




                  • robustness and

                  • security


                  Both are based on the same underlying principle: in general, you simply cannot know what the current directory is, when your code is run. Which means that, when you require a file and depend on it being in the current directory, you have no way of controlling whether that file will even be there, or whether it is the file that you actually expect to be there.






                  share|improve this answer



















                  • 5





                    I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                    – John Feminella
                    May 25 '10 at 12:11






                  • 4





                    @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                    – Jörg W Mittag
                    May 25 '10 at 12:53













                  • So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                    – Joshua Cheek
                    Sep 11 '10 at 22:07






                  • 4





                    @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                    – Jörg W Mittag
                    Sep 11 '10 at 22:22








                  • 8





                    @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                    – Jörg W Mittag
                    Sep 11 '10 at 22:24
















                  34














                  There's two reasons:




                  • robustness and

                  • security


                  Both are based on the same underlying principle: in general, you simply cannot know what the current directory is, when your code is run. Which means that, when you require a file and depend on it being in the current directory, you have no way of controlling whether that file will even be there, or whether it is the file that you actually expect to be there.






                  share|improve this answer



















                  • 5





                    I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                    – John Feminella
                    May 25 '10 at 12:11






                  • 4





                    @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                    – Jörg W Mittag
                    May 25 '10 at 12:53













                  • So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                    – Joshua Cheek
                    Sep 11 '10 at 22:07






                  • 4





                    @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                    – Jörg W Mittag
                    Sep 11 '10 at 22:22








                  • 8





                    @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                    – Jörg W Mittag
                    Sep 11 '10 at 22:24














                  34












                  34








                  34







                  There's two reasons:




                  • robustness and

                  • security


                  Both are based on the same underlying principle: in general, you simply cannot know what the current directory is, when your code is run. Which means that, when you require a file and depend on it being in the current directory, you have no way of controlling whether that file will even be there, or whether it is the file that you actually expect to be there.






                  share|improve this answer













                  There's two reasons:




                  • robustness and

                  • security


                  Both are based on the same underlying principle: in general, you simply cannot know what the current directory is, when your code is run. Which means that, when you require a file and depend on it being in the current directory, you have no way of controlling whether that file will even be there, or whether it is the file that you actually expect to be there.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 25 '10 at 7:17









                  Jörg W MittagJörg W Mittag

                  292k63356551




                  292k63356551








                  • 5





                    I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                    – John Feminella
                    May 25 '10 at 12:11






                  • 4





                    @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                    – Jörg W Mittag
                    May 25 '10 at 12:53













                  • So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                    – Joshua Cheek
                    Sep 11 '10 at 22:07






                  • 4





                    @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                    – Jörg W Mittag
                    Sep 11 '10 at 22:22








                  • 8





                    @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                    – Jörg W Mittag
                    Sep 11 '10 at 22:24














                  • 5





                    I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                    – John Feminella
                    May 25 '10 at 12:11






                  • 4





                    @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                    – Jörg W Mittag
                    May 25 '10 at 12:53













                  • So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                    – Joshua Cheek
                    Sep 11 '10 at 22:07






                  • 4





                    @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                    – Jörg W Mittag
                    Sep 11 '10 at 22:22








                  • 8





                    @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                    – Jörg W Mittag
                    Sep 11 '10 at 22:24








                  5




                  5





                  I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                  – John Feminella
                  May 25 '10 at 12:11





                  I don't think that enforcing that two files be in the same location relative to each other is necessarily a bad requirement. If that were true, then we would have no use for directories.

                  – John Feminella
                  May 25 '10 at 12:11




                  4




                  4





                  @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                  – Jörg W Mittag
                  May 25 '10 at 12:53







                  @John Feminella: what does this have to do with putting files in paths relative to each other? The question is about putting them relative to ., i.e. the current working directory. If the user cd s into a different directory, the current working directory changes, and you now require completely different files depending on what directory the user happened to be in when he called your script. I don't think that's a good idea.

                  – Jörg W Mittag
                  May 25 '10 at 12:53















                  So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                  – Joshua Cheek
                  Sep 11 '10 at 22:07





                  So to maintain a decent interface, you should do this? $: << File.dirname(__FILE__)

                  – Joshua Cheek
                  Sep 11 '10 at 22:07




                  4




                  4





                  @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                  – Jörg W Mittag
                  Sep 11 '10 at 22:22







                  @Joshua Cheek: Personally, I don't like that. (But please don't look at my older code, because it is littered with that kind of stuff :-) ). I simply pretend that the lib directory is on the $LOAD_PATH and then require all files relative to lib. In other words: I leave it to the administrator to figure out how to set up the $LOAD_PATH correctly. If you use RubyGems, that is trivial, because RubyGems automatically does it for you, and if you use Debian packages, then it's the package maintainer's job. All in all, it seems to work out quite nicely.

                  – Jörg W Mittag
                  Sep 11 '10 at 22:22






                  8




                  8





                  @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                  – Jörg W Mittag
                  Sep 11 '10 at 22:24





                  @Joshua Cheek: Also, as a sort-of counterbalance to removing . from $LOAD_PATH, Ruby 1.9.2 introduces require_relative which ... surprise ... require s a file relative to the location of the currently executing file (i.e. relative to File.dirname(__FILE__) ).

                  – Jörg W Mittag
                  Sep 11 '10 at 22:24











                  16














                  As others answers point out, it's a security risk because . in your load path refers to the present working directory Dir.pwd, not the directory of the current file being loaded. So whoever is executing your script can change this simply by cding to another directory. Not good!



                  I've been using full paths constructed from __FILE__ as an alternative.



                  require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))


                  Unlike require_relative, this is backward compatible with Ruby 1.8.7.






                  share|improve this answer



















                  • 4





                    There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                    – Tyler Rick
                    May 6 '11 at 23:12


















                  16














                  As others answers point out, it's a security risk because . in your load path refers to the present working directory Dir.pwd, not the directory of the current file being loaded. So whoever is executing your script can change this simply by cding to another directory. Not good!



                  I've been using full paths constructed from __FILE__ as an alternative.



                  require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))


                  Unlike require_relative, this is backward compatible with Ruby 1.8.7.






                  share|improve this answer



















                  • 4





                    There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                    – Tyler Rick
                    May 6 '11 at 23:12
















                  16












                  16








                  16







                  As others answers point out, it's a security risk because . in your load path refers to the present working directory Dir.pwd, not the directory of the current file being loaded. So whoever is executing your script can change this simply by cding to another directory. Not good!



                  I've been using full paths constructed from __FILE__ as an alternative.



                  require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))


                  Unlike require_relative, this is backward compatible with Ruby 1.8.7.






                  share|improve this answer













                  As others answers point out, it's a security risk because . in your load path refers to the present working directory Dir.pwd, not the directory of the current file being loaded. So whoever is executing your script can change this simply by cding to another directory. Not good!



                  I've been using full paths constructed from __FILE__ as an alternative.



                  require File.expand_path(File.join(File.dirname(__FILE__), 'filename'))


                  Unlike require_relative, this is backward compatible with Ruby 1.8.7.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 18 '11 at 6:50









                  Jonathan TranJonathan Tran

                  11k85263




                  11k85263








                  • 4





                    There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                    – Tyler Rick
                    May 6 '11 at 23:12
















                  • 4





                    There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                    – Tyler Rick
                    May 6 '11 at 23:12










                  4




                  4





                  There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                  – Tyler Rick
                  May 6 '11 at 23:12







                  There's also this variation (which I personally find more readable): require Pathname.new(__FILE__).dirname + 'filename'

                  – Tyler Rick
                  May 6 '11 at 23:12













                  8














                  Use require_relative 'file_to_require'



                  Throw this in your code to make require_relative work in 1.8.7:



                  unless Kernel.respond_to?(:require_relative)
                  module Kernel
                  def require_relative(path)
                  require File.join(File.dirname(caller.first), path.to_str)
                  end
                  end
                  end





                  share|improve this answer






























                    8














                    Use require_relative 'file_to_require'



                    Throw this in your code to make require_relative work in 1.8.7:



                    unless Kernel.respond_to?(:require_relative)
                    module Kernel
                    def require_relative(path)
                    require File.join(File.dirname(caller.first), path.to_str)
                    end
                    end
                    end





                    share|improve this answer




























                      8












                      8








                      8







                      Use require_relative 'file_to_require'



                      Throw this in your code to make require_relative work in 1.8.7:



                      unless Kernel.respond_to?(:require_relative)
                      module Kernel
                      def require_relative(path)
                      require File.join(File.dirname(caller.first), path.to_str)
                      end
                      end
                      end





                      share|improve this answer















                      Use require_relative 'file_to_require'



                      Throw this in your code to make require_relative work in 1.8.7:



                      unless Kernel.respond_to?(:require_relative)
                      module Kernel
                      def require_relative(path)
                      require File.join(File.dirname(caller.first), path.to_str)
                      end
                      end
                      end






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 29 '13 at 18:20

























                      answered Jan 25 '13 at 13:50









                      Tyler BrockTyler Brock

                      21.4k116369




                      21.4k116369























                          6














                          '.' in your path has long been considered a bad thing in the Unix world (see, for example, http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html). I assume the Ruby folks have been persuaded of the wisdom of not doing that.






                          share|improve this answer




























                            6














                            '.' in your path has long been considered a bad thing in the Unix world (see, for example, http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html). I assume the Ruby folks have been persuaded of the wisdom of not doing that.






                            share|improve this answer


























                              6












                              6








                              6







                              '.' in your path has long been considered a bad thing in the Unix world (see, for example, http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html). I assume the Ruby folks have been persuaded of the wisdom of not doing that.






                              share|improve this answer













                              '.' in your path has long been considered a bad thing in the Unix world (see, for example, http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html). I assume the Ruby folks have been persuaded of the wisdom of not doing that.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 25 '10 at 7:32









                              RodgerRodger

                              31212




                              31212























                                  3














                                  I found this to be a confounding change until I realized a couple of things.



                                  You can set RUBYLIB in your .profile (Unix) and go on with life as you did before:



                                  export RUBYLIB="."



                                  But as mentioned above, it's long been considered unsafe to do so.



                                  For the vast majority of cases you can avoid problems by simply calling your Ruby scripts with a prepended '.' e.g. ./scripts/server.






                                  share|improve this answer




























                                    3














                                    I found this to be a confounding change until I realized a couple of things.



                                    You can set RUBYLIB in your .profile (Unix) and go on with life as you did before:



                                    export RUBYLIB="."



                                    But as mentioned above, it's long been considered unsafe to do so.



                                    For the vast majority of cases you can avoid problems by simply calling your Ruby scripts with a prepended '.' e.g. ./scripts/server.






                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      I found this to be a confounding change until I realized a couple of things.



                                      You can set RUBYLIB in your .profile (Unix) and go on with life as you did before:



                                      export RUBYLIB="."



                                      But as mentioned above, it's long been considered unsafe to do so.



                                      For the vast majority of cases you can avoid problems by simply calling your Ruby scripts with a prepended '.' e.g. ./scripts/server.






                                      share|improve this answer













                                      I found this to be a confounding change until I realized a couple of things.



                                      You can set RUBYLIB in your .profile (Unix) and go on with life as you did before:



                                      export RUBYLIB="."



                                      But as mentioned above, it's long been considered unsafe to do so.



                                      For the vast majority of cases you can avoid problems by simply calling your Ruby scripts with a prepended '.' e.g. ./scripts/server.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 1 '11 at 21:45









                                      DylanDylan

                                      3,33311412




                                      3,33311412























                                          3














                                          As Jörg W Mittag pointed out, I think what you want to be using is require_relative so the file you require is relative to the source file of the require declaration and not the current working dir.



                                          Your dependencies should be relative to your rake build file.






                                          share|improve this answer






























                                            3














                                            As Jörg W Mittag pointed out, I think what you want to be using is require_relative so the file you require is relative to the source file of the require declaration and not the current working dir.



                                            Your dependencies should be relative to your rake build file.






                                            share|improve this answer




























                                              3












                                              3








                                              3







                                              As Jörg W Mittag pointed out, I think what you want to be using is require_relative so the file you require is relative to the source file of the require declaration and not the current working dir.



                                              Your dependencies should be relative to your rake build file.






                                              share|improve this answer















                                              As Jörg W Mittag pointed out, I think what you want to be using is require_relative so the file you require is relative to the source file of the require declaration and not the current working dir.



                                              Your dependencies should be relative to your rake build file.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Dec 31 '13 at 4:31









                                              Bad Request

                                              1,95552535




                                              1,95552535










                                              answered Feb 1 '11 at 11:15









                                              martomarto

                                              12115




                                              12115






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2900370%2fwhy-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Create new schema in PostgreSQL using DBeaver

                                                  Deepest pit of an array with Javascript: test on Codility

                                                  Fotorealismo