Ex 15 - To READ or NOT TO READ

I followed the Exercise code as give

filename = ARGV.first

txt = open(filename)

puts "Here's your file #{filename}!"
print txt.read

print "Type the filename again:"
file_again = $stdin.gets.chomp

txt_again = open(file_again)

print txt_again.read

and got this error:

I tried looking up ways this might be accomplished, since Ruby has changed some, since the book was written, but nothing has helped. Any ideas?

1 Like

So you need to make sure that file is in the current directory. You’re running a ruby script that’s way over in the E:\ drive but your account is currently in C:\Users\Al. Where ruby thinks to look is NOT based on where the ruby script is located. It’s based on where YOU are located. So type this:


First command tells you where you are. Second “lists” the contents of that directory. If you don’t see ex15_sample.txt there then you’re in the wrong place. cd to the right place.

LOL! I never knew it was the specific. Thanks!

A free service run by Zed A. Shaw for learncodethehardway.org.