Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中File.open的问题_Ruby - Fatal编程技术网

Ruby中File.open的问题

Ruby中File.open的问题,ruby,Ruby,我目前正试图从《艰苦学习Ruby》一书中自学Ruby,在我进行练习时,有人告诉我运行“ri File.open”并阅读一些文档。完成此操作后,我无法从任何程序运行File.open。每当我这样做时,我都会收到以下信息: Heres your file: ex15_sample.txt = File.open (from ruby core) -----------------------------------------------------------------------------

我目前正试图从《艰苦学习Ruby》一书中自学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.

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

听起来您可能陷入了ri

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

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

关于您的代码,您对
文件使用了错误的做法。请打开
。在块退出时使用块自动关闭文件。这是良好的内务管理,在写入文件时尤其重要:

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



您看到的另一个可能原因是,您列出的文件实际上是您输入的
ri
命令的输出,您实际上只是重复地读取和打印该文件

您是否只是使用了
文件。在没有参数的情况下打开
?该输出告诉您需要参数。例如,
File.open('myfile.txt')
将打开
myfile.txt
。如果你用谷歌搜索“Ruby File.open examples”,你会发现很多例子。我已经把我正在运行的程序添加到了这个问题中。我还添加了运行这个程序时得到的确切结果。我使用CNTRL+D,因为它是关闭/退出命令行“get”循环的标准方法。Enter/Return不能保证做任何事情。嘿,谢谢你的建议,经过调查并尝试卸载/重新安装ruby后,我发现文本文件被ri输出覆盖了。我真的很傻没有检查,但谢谢你的帮助。也谢谢你的代码建议。
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