-1

我目前正在尝试通过“Learning Ruby the Hard Way”一书自学 Ruby,并且在进行练习时,有人告诉我运行“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)

4

1 回答 1

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您输入的命令的输出,而您实际上只是在重复阅读和打印。

于 2013-09-13T13:34:05.057 回答