3

Basically, I'm really stuck.

I've got this text where I need to do like this: *print prompt file_again = STDIN.gets.chomp() txt_again = File.open(file_again) puts txt_again.read()*

and basically get text from .txt file printed on my console!

Using File.open() directly from irb, but then attempting:

 File.open("ex15_sample.txt")

^ I assume it opens but I still end up nowhere. I mean, it's not marked as a variable and I can't print it.

If I'll use:

txt = File.open("ex15_sample.txt")

I'll get some error in the first place, so I can't use print txt later on.

Exercise is from http://ruby.learncodethehardway.org/book/ex15.html and I'm trying to do optional stuff so I don't end up nowhere as with codeschool beginners lesson I did earlier on.

tshepang
  • 12,111
  • 21
  • 91
  • 136
5brickbomzh
  • 99
  • 1
  • 4
  • 12
  • 4
    Hi. "some error" is difficult to help with - if you can be more specific you should get an answer – Frederick Cheung Dec 12 '12 at 07:26
  • It actually doesn't. It actually doesn't do anything at all. The code is like $ txt = File.open("ex15_sample.txt"); puts txt.read(); all the irb does is increases it's number oh and after 2 enters afterwards it change > to * at it's end. Whatever that means – 5brickbomzh Dec 12 '12 at 08:05
  • it's because of your trailing semicolon – Frederick Cheung Dec 12 '12 at 08:18
  • 1
    Are you running irb from the same directory that the `ex15_sample.txt` file is in? You'll need to `cd` to that directory in the terminal first before you can call `txt = File.open("ex15_sample.txt")` in irb. Or you can use the full path to the file. – Nick Dec 12 '12 at 08:27
  • I laughed when I read hashtag Ruby :) – squiguy Dec 12 '12 at 09:11
  • See also http://stackoverflow.com/questions/13837722/what-is-the-class-of-if-unless-etc/13839206#13839206 – BernardK Dec 12 '12 at 12:03

2 Answers2

18

I have created a file ex15_sample.txt in .../Ruby/zintlist/irb.

1.8.6 :082 > File.open("ex15_sample.txt")
Errno::ENOENT: No such file or directory - ex15_sample.txt
    from (irb):82:in `initialize'
    from (irb):82:in `open'
    from (irb):82
    from :0
1.8.6 :086 > Dir.getwd
 => "/.../Ruby/prod/spec" 
1.8.6 :087 > Dir.chdir('../../zintlist/irb')
 => 0 
1.8.6 :088 > Dir.getwd
 => "/.../Ruby/zintlist/irb" 
1.8.6 :089 > File.open("ex15_sample.txt")
 => #<File:ex15_sample.txt> 
1.8.6 :090 > 

attempting File.open("ex15_sample.txt") I assume it opens

Within irb, usually you don't need to assume, you have an immediate answer.

1.8.6 :090 > txt = File.open("ex15_sample.txt")
 => #<File:ex15_sample.txt> 
1.8.6 :091 > puts txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
 => nil 
1.8.6 :092 > 
BernardK
  • 3,674
  • 2
  • 15
  • 10
0
1.8.6 :090 > txt = open("ex15_sample.txt")
 => #<File:ex15_sample.txt> 
1.8.6 :091 > puts txt.read
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
 => nil 
1.8.6 :092 > 
Jowe
  • 1
  • 1
    Welcome to Stack Overflow! While you may have solved the asker's problem, code-only answers are not very helpful to others who come across this question. Please edit your answer to explain why your code solves the original problem. – Joe C Jan 22 '17 at 22:21