Re: [External] re: Adding a directory to LOAD_PATH
Martin DeMello <[email protected]> Thu, 7 Nov 2019 11:33:13 -0800
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAFrFfuFk+m0o16ApG6L1A_2fm8vuUpM=dL8g3NUXS+WTWFQddg@mail.gmail.com> |
--===============0839741182== Content-Type: multipart/alternative; boundary="0000000000002135d20596c6bdf9" --0000000000002135d20596c6bdf9 Content-Type: text/plain; charset="UTF-8" On Thu, Nov 7, 2019 at 9:42 AM Tran, Minh <[email protected]> wrote: > Thank you so much Leam. > I would like to load the kinetic-sdk.rb program in > /vendor/kinetic-sdk-rb/ to my current working directory /my_project ; > so would the following command work ? > > $LOAD_PATH << File.expand_path("/vendor/kinectic-sdk-rb", __FILE__) > File.expand_path converts things like ".." and "~" to absolute paths; since you already have an absolute path you don't need it here. You can just say $LOAD_PATH << "/vendor/kinetic-sdk-rb". Also, if you want to load the file as require "kinetic-sdk-rb/kinetic-sdk.rb" then you want only /vendor in your $LOAD_PATH, not /vendor/kinetic-sdk-rb - the require path is relative to the LOAD_PATH entry. Here's an example: $ mkdir -p /tmp/ruby/foo $ cat > /tmp/ruby/foo/bar.rb def f puts "hello world" end $ irb irb(main):001:0> $LOAD_PATH << "/tmp/ruby" => ["/usr/share/rubygems-integration/all/gems/did_you_mean-1.2.1/lib", "/usr/local/lib/site_ruby/2.5.0", "/usr/local/lib/x86_64-linux-gnu/site_ruby", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/2.5.0", "/usr/lib/x86_64-linux-gnu/ruby/vendor_ruby/2.5.0", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/2.5.0", "/usr/lib/x86_64-linux-gnu/ruby/2.5.0", "/tmp/ruby"] irb(main):002:0> require 'foo/bar' => true irb(main):003:0> f hello world martin --0000000000002135d20596c6bdf9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr">On Thu, Nov 7, 2019 at 9:42 AM Tran, Minh= <<a href=3D"mailto:[email protected]">minh.tran@emoryhealth= care.org</a>> wrote:<br></div><div class=3D"gmail_quote"><blockquote cla= ss=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid = rgb(204,204,204);padding-left:1ex"> <div dir=3D"ltr"> <div style=3D"color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-ser= if;font-size:12pt"> Thank you so much Leam. </div> <div style=3D"color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-ser= if;font-size:12pt"> I would like to=C2=A0load the kinetic-sdk.rb program in /vendor/kinetic-sdk= -rb/=C2=A0=C2=A0to my current working directory /my_project ; </div> <div style=3D"color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-ser= if;font-size:12pt"> so would the following command work ?</div> <div style=3D"color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-ser= if;font-size:12pt"> <br> </div> <div style=3D"color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-ser= if;font-size:12pt"> <font size=3D"3">$LOAD_PATH << File.expand_path("/vendor/kinecti= c-sdk-rb", __FILE__)</font>=C2=A0 =C2=A0</div></div></blockquote><div>= <br></div><div>File.expand_path converts things like ".." and &qu= ot;~" to absolute paths; since you already have an absolute path you d= on't need it here. You can just say $LOAD_PATH << "/vendor/k= inetic-sdk-rb".</div><div><br></div><div>Also, if you want to load the= file as require "kinetic-sdk-rb/kinetic-sdk.rb" then you want on= ly /vendor in your $LOAD_PATH, not /vendor/kinetic-sdk-rb - the require pat= h is relative to the LOAD_PATH entry. Here's an example:</div><div><br>= </div><div><font face=3D"monospace"><br></font></div><div><font face=3D"mon= ospace">$ mkdir -p /tmp/ruby/foo<br><br></font></div><div><font face=3D"mon= ospace">$ cat > /tmp/ruby/foo/bar.rb<br>def f =C2=A0 <br>=C2=A0 puts &qu= ot;hello world"<br>end<br><br></font></div><div><font face=3D"monospac= e">$ irb<br>irb(main):001:0> $LOAD_PATH << "/tmp/ruby"<b= r>=3D> ["/usr/share/rubygems-integration/all/gems/did_you_mean-1.2.= 1/lib", "/usr/local/lib/site_ruby/2.5.0", "/usr/local/l= ib/x86_64-linux-gnu/site_ruby", "/usr/local/lib/site_ruby", = "/usr/lib/ruby/vendor_ruby/2.5.0", "/usr/lib/x86_64-linux-gn= u/ruby/vendor_ruby/2.5.0", "/usr/lib/ruby/vendor_ruby", &quo= t;/usr/lib/ruby/2.5.0", "/usr/lib/x86_64-linux-gnu/ruby/2.5.0&quo= t;, "/tmp/ruby"]<br>irb(main):002:0> require 'foo/bar'= <br>=3D> true<br>irb(main):003:0> f<br>hello world</font><br></div><d= iv><br></div><div>martin=C2=A0</div></div></div> --0000000000002135d20596c6bdf9-- --===============0839741182== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk> --===============0839741182==--