How to use open() in irb console to print out basic text from .txt file? #Ruby

cmd, irb, ruby

Solution

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 > 

Problem

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.

Original source

Related problems