0

I am doing a series of tutorials on how to code in Ruby. I want to read a .txt file using this formula:

filename = ARGV.first

prompt = "> "
txt = File.open(filename)

puts "Here's your file: #{filename}"
puts txt.read()

puts "I'll also ask you to type it again:"
print prompt
file_again = STDIN.gets.chomp()

txt_again = File.open(file_again)

puts txt_again.read()

The text file reads:

This is stuff I typed into a file. It is really cool stuff.
Lots and lots of fun to have in here.

The name for the text file is ex15_sample.txt. I tried with the above formula, and nothing seems to work. I have a hard understanding how to use both ARGV and STDIN.gets.chomp.

What should I do? I ask that you use the formula above; this stuff is a little confusing, so for now, just use the formula above.

5
  • What do you mean by "specific script"? Do you mean "encoding"? And what is a "formula"? Commented Apr 26, 2013 at 14:06
  • 1
    How are you running the program: what exactly are you typing on the command line? And when you say nothing seems to work, what exactly do you mean: are there error messages? Commented Apr 26, 2013 at 14:17
  • @Jorg See, I don't know a whole lot of terms regarding what it is I'm doing. I'm a beginner...and I only know very basic things, so all of this is completely new to me. But to answer your questions: I don't know what I mean't by specific script since my question has been edited. I think that encoding what I might be trying to do. I say formula as in the one that says "ARGV.first, etc.. etc... Like I said, completely new. Commented Apr 26, 2013 at 17:14
  • @FMc Well, where it says "filename = ARGV.first" I fill in the filename with "ex15_sample.txt" and it gives me error messages all the time. So yes, every time I try to do something new, I just get error messages out of the yin yang. Commented Apr 26, 2013 at 17:16
  • @Jorg What I mean't by specific script is, the big script that reads "filename = ARGV.first, etc...etc.." Sorry, I forgot about the main question above. Commented Apr 26, 2013 at 17:18

1 Answer 1

2

The script works. You're not explaining how you're trying to run the script or what errors you're seeing, so it's a bit hard to help you.

If you have a text file named ex15_sample.txt in the same directory as your script (let's call it script.rb), and if you have Ruby set up properly, then if you run it with

$ ruby script.rb ex15_sample.txt

everything should work fine.

If you're trying to change the first line to always use ex15_sample.txt, be sure to put it in quotes:

filename = "ex15_sample.txt" # Without the quotes, you'll get an error.

Again, it's hard to help you without knowing exactly how you're running the script or what errors you're getting.

Update: I seems your issue is that you aren't clear on how to run a Ruby script. The simplest way is to, at your system's command prompt, type ruby then a space, then the name of the file with a Ruby script in it. If your script is in a file named script.rb, you would type ruby script.rb. That won't work if your script is in a file with a different name. If the script is in a file named read-a-file.rb, then you need to type ruby read-a-file.rb.

This particular script wants a command line argument after the file name. If the text file you want to read is in a file named ex15_sample.txt, then you need to type that after the script name. In the previous example, the command would become ruby read-a-file.rb ex15_sample.txt. That will only work if the files are in the same directory (a.k.a. folder).

Sign up to request clarification or add additional context in comments.

12 Comments

Ok, lemme try it, and I'll come back to ya with my error messages.
Well, lemme ask you this question. When you say, if you run it with $ ruby script.rb ex15_sample.txt that it should work, what exactly do you mean? Do you mean from the command prompt? Like for ex: I type in irb, then type in what you put above and then should I get no error messages. Maybe that's the problem...maybe I dunno what to do to begin with. Basically I'm just trying to get Ruby to read that .txt file, that's all I'm really doing.
What floors me too is, I didn't have a problem with any of the tutorials up until this point. I don't get it.
Usin the script as it stands, trying to get the file name from ARGV, doesn't really make sense in irb. You would type ruby script.rb ex15_sample.txt instead of typing irb at the command prompt if you want to run the script in a file called script.rb. If you just want Ruby to read the file from within irb, you could just do File.read('ex15_sample.txt').
Well...lemme type in just ruby script.rb ex15_sample.txt in the command prompt and see what happens.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.