Ruby中的File.open问题

时间:2013-09-13 13:14:28

标签: ruby

我目前正在尝试从书中学习Ruby,“学习Ruby the Hard Way”,并且,当我进行练习时,我被告知要运行“ri File.open”并阅读一些文档。在这之后,我无法从任何程序运行File.open。每当我这样做,我都会收到这样的信息:

Heres your file: ex15_sample.txt
= File.open

(from ruby core)
------------------------------------------------------------------------------
  File.open(filename, mode="r" [, opt])                 -> file
  File.open(filename [, mode [, perm]] [, opt])         -> file
  File.open(filename, mode="r" [, opt]) {|file| block } -> obj
  File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj


------------------------------------------------------------------------------

With no associated block, File.open is a synonym for File.new. If the optional
code block is given, it will be passed the opened file as an argument, and the
File object will automatically be closed when the block terminates.  In this
instance, File.open returns the value of the block.

See IO.new for a list of values for the opt parameter.


Ill also ask you to type it again:
> ^Cex15.rb:13:in `gets': Interrupt
    from ex15.rb:13:in `<main>'

amelia@Amelia:~/Documents$ clear

amelia@Amelia:~/Documents$ ruby ex15.rb ex15_sample.txt
Here's your file: ex15_sample.txt
= File.open

(from ruby core)
------------------------------------------------------------------------------
  File.open(filename, mode="r" [, opt])                 -> file
  File.open(filename [, mode [, perm]] [, opt])         -> file
  File.open(filename, mode="r" [, opt]) {|file| block } -> obj
  File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj


------------------------------------------------------------------------------

With no associated block, File.open is a synonym for File.new. If the optional
code block is given, it will be passed the opened file as an argument, and the
File object will automatically be closed when the block terminates.  In this
instance, File.open returns the value of the block.

See IO.new for a list of values for the opt parameter.


Ill also ask you to type it again:
> ex15_sample.txt
= File.open

(from ruby core)
------------------------------------------------------------------------------
  File.open(filename, mode="r" [, opt])                 -> file
  File.open(filename [, mode [, perm]] [, opt])         -> file
  File.open(filename, mode="r" [, opt]) {|file| block } -> obj
  File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj


------------------------------------------------------------------------------

With no associated block, File.open is a synonym for File.new. If the optional
code block is given, it will be passed the opened file as an argument, and the
File object will automatically be closed when the block terminates.  In this
instance, File.open returns the value of the block.

See IO.new for a list of values for the opt parameter.

任何人都知道如何解决这个问题?

我正在运行的程序是:

filename = ARGV.first

prompt = '> '

txt = File.open(filename)

puts"Here's your file: #{filename}"
puts txt.read()

puts "I'll also ask you to type it again:"
print prompt

file_again = STDIN.gets.chomp()

txt_again = File.open(file_again)

puts txt_again.read()

将ARGV.first设置为文本文件(特别是ex15_sample.txt)

1 个答案:

答案 0 :(得分:0)

听起来你可能会陷入ri。

尝试按 CNTRL + D 返回shell提示符。

如果您输入ri File.open作为两个单独的命令,则会发生这种情况。第一个ri以交互模式打开ri并将您置于其命令行中。此时File.open显示您输入的任何Ruby类和方法的帮助。

关于您的代码,您使用File.open的不良做法。使用带有块的块可在块退出时自动关闭文件。这是很好的内务管理,在写入文件时尤为重要:

File.open(filename) do |txt|
  puts"Here's your file: #{filename}"
  puts txt.read()
end

File.open(file_again) do |txt_again|
  puts txt_again.read()
end

您所看到的另一个可能原因是您列出的文件实际上是您输入的ri命令的输出,而您实际上只是反复阅读和打印。