Forum: Ruby require -- looking in rubygems, not "."

Posted by charles a. (charles_a)
on 2011-10-10 01:18
irb
require "tr.rb"
LoadError: no such file to load -- tr.rb
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>"29"in `require'
from (irb):4
from C:/Ruby192/bin/irb:12:in `<main>'

load "tr.rb"
Hello world!
=> true

File.exist? "tr.rb"
=> true

APP_ROOT  = File.dirname(__FILE__)
=> "."
require "#{APP_ROOT}/tr.rb
LoadError: no such file to load -- ./practice
from....
(same stuff about ruby gems)

Trying to get started learning ruby. From what little I can tell, I
think Irb is only looking in rubygems instead of its own directory.
How do I fix that? This is my first attempt to learn any sort of
programming. Tyvm in advance for any help.
Posted by Steve Klabnik (Guest)
on 2011-10-10 02:10
(Received via mailing list)
In Ruby 1.9.2, the current directory isn't in the load path. Use
require_relative instead.
Posted by charles a. (charles_a)
on 2011-10-10 03:57
Thank you very much for the help. Could someone be so kind as to show an 
example of what that code should look like? I tried the following 
without success, all gave error messages.

with the command prompt in the desktop, so the prompt says " 
C:\Users\Charles\Desktop , then started irb, so the prompt says 
irb(main):001:0

require_relative 'tr'
require_relative "tr"
require_relative "tr.rb"
require_relative "Desktop/tr"
require_relative "./tr.rb"
require_relative "./tr"
requre_relative "desktop/tr"


and so on, approximately 30 other variations, including substituting 
backslashes for slashes, although in the past I have found that forward 
slashes work in ruby on this computer while backslashes sometimes don't. 
I googled "require_relative" and found some examples, it looks like the 
first one above should have worked. I also tried restarting the computer 
and doing it all over again, then tried it on a laptop with winxp. None 
of these worked either. Thanks in advance if someone has a minute to 
answer this.
Posted by Steve Klabnik (Guest)
on 2011-10-10 04:11
(Received via mailing list)
$ ls
tr.rb
$ irb
> require_relative "tr"
#=> true
Posted by charles a. (charles_a)
on 2011-10-10 04:51
Thanks very much again. I really appreciate the help.

I tried typing the green line, but without the " > " symbol, then tried 
it with the " > ". I tried typing the line

$ ls

 and got an error message and tried it with a one instead of a letter l, 
and tried all the other lines. I tried it on two other computers, in all 
cases all I got was error messages. What am I doing wrong? Tyvm in 
advance, and apologies for being so obtuse.
Posted by Josh Cheek (josh-cheek)
on 2011-10-10 04:59
(Received via mailing list)
On Sun, Oct 9, 2011 at 9:51 PM, charles a. 
<charles.agriesti@gmail.com>wrote:

> advance, and apologies for being so obtuse.
>
>
"ls" is the Unix tool to list the contents of a directory. The 
equivalent on
Windows is "dir"
Posted by Josh Cheek (josh-cheek)
on 2011-10-10 05:02
(Received via mailing list)
On Sun, Oct 9, 2011 at 9:58 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

>> and tried all the other lines. I tried it on two other computers, in all
>> cases all I got was error messages. What am I doing wrong? Tyvm in
>> advance, and apologies for being so obtuse.
>>
>>
> "ls" is the Unix tool to list the contents of a directory. The equivalent
> on Windows is "dir"
>
>
The point he's making is that there is a file in the current directory 
named
"tr.rb"
Posted by charles a. (charles_a)
on 2011-10-10 05:17
Thank you very much for taking time to reply sir. I am watching your 
videos right now.

I still do not know what to type so to get the program to require that 
file, so that any data or code in it, in the file tr.rb, and be able to 
use it in the program.

recapping from above, I have tried typing at the irb prompt:


require_relative "tr"
require_relative "tr.rb"
require_relative "./tr"

and every variation I can think of.

So the question I mean to ask is what do I type at the irb prompt to 
make that happen?
tyvm in advance and thanks all for your patience.
Posted by Josh Cheek (josh-cheek)
on 2011-10-10 06:28
(Received via mailing list)
On Sun, Oct 9, 2011 at 10:17 PM, charles a. 
<charles.agriesti@gmail.com>wrote:

> require_relative "tr"
> Posted via http://www.ruby-forum.com/.
>
>
Here, I have made a video showing how to use it 
http://vimeo.com/30294384
Posted by Josh Cheek (josh-cheek)
on 2011-10-10 06:41
(Received via mailing list)
On Sun, Oct 9, 2011 at 11:28 PM, Josh Cheek <josh.cheek@gmail.com> 
wrote:

>
> Here, I have made a video showing how to use it http://vimeo.com/30294384
>
>
Sorry, "technical difficulties". Video is here instead
http://vimeo.com/30295272
Posted by charles a. (charles_a)
on 2011-10-10 14:27
I cannot thank you enough. Josh Cheek rocks. Thanks also to Steve 
Klabnik.

I thought I had tried  < require './tr' >  last night and it didn't 
work. It unquestionably does work this morning.

Scrolling up in the terminal window, which is still running, I see I did 
try it last night and got error messages. I cannot explain that, but at 
least it works now.

Thanks again.

Back to watching Josh's videos.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.