0

ローカルの xml ファイルを開いて、そのコンテンツをターミナルに出力しようとしています。

私はこれを試しました;

puts File.new('file.xml', 'r')

この;

puts File.open('file.xml', 'r')

xmlファイルを画面に出力する代わりに、両方からの出力です。

#<File:0x00000000....>
4

2 に答える 2

4

これを試して

puts File.read('file.xml')

また

puts File.open('file.xml').read

ドキュメント: IO.readIO#read

于 2013-11-10T14:30:15.443 に答える
1

block with File#openmethod を使用することをお勧めします。ブロックと同様に、ファイルを明示的に閉じる必要はありません。ファイルを使用してブロック内のすべてのタスクを実行します。ブロックが終了すると、ファイルは自動的に閉じられます。

File.open('doc.txt','r') do |file|
  puts file.read
end

# >> ,"11: Agriculture, Forestry, Fishing and Hunting",,
# >> ,,"111: Crop Production",
# >> ,,,"111110: Soybean Farming"
# >> ,,,"111120: Oilseed (except Soybean) Farming"
于 2013-11-10T15:26:24.913 に答える