我目前正在尝试通过“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)