基本上,我真的被卡住了.
我有这样的文字,我需要这样做: *print prompt file_again = STDIN.gets.chomp()txt_again = File.open(file_again)puts txt_again.read()*
并且基本上从我的控制台上打印的.txt文件中获取文本!
直接从irb使用File.open(),但后来尝试:
File.open("ex15_sample.txt")
Run Code Online (Sandbox Code Playgroud)
我认为它打开但我仍然无处可去.我的意思是,它没有标记为变量,我无法打印它.
如果我将使用:
txt = File.open("ex15_sample.txt")
Run Code Online (Sandbox Code Playgroud)
我首先会得到一些错误,所以我以后不能使用print txt.
练习来自http://ruby.learncodethehardway.org/book/ex15.html,我正在尝试做一些可选的东西,所以我不会像我之前做过的代码学校初学者课程一样无处可去.
Ber*_*rdK 17
我在.../Ruby/zintlist/irb中创建了一个文件ex15_sample.txt.
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 >
Run Code Online (Sandbox Code Playgroud)
尝试File.open("ex15_sample.txt")我假设它打开了
在irb中,通常你不需要假设,你有一个直接的答案.
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 >
Run Code Online (Sandbox Code Playgroud)