2

基本的に、私は本当に立ち往生しています。

私はこのようにする必要があるところにこのテキストを持っています: * 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")

^私はそれが開くと思いますが、私はまだどこにも行き着きません。つまり、変数としてマークされておらず、印刷できません。

使用する場合:

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

そもそもエラーが発生するので、後でprinttxtを使用することはできません。

演習はhttp://ruby.learncodethehardway.org/book/ex15.htmlからのものであり、オプションの作業をしようとしているので、以前に行ったコードスクールの初心者向けレッスンのようにどこにも行き着きません。

4

2 に答える 2

18

... / 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 > 

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 > 
于 2012-12-12T08:54:25.633 に答える
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 > 
于 2017-01-22T22:01:47.507 に答える