为什么我有“未定义的方法”存在于文件:类?

时间:2022-08-22 22:34:40

I just installed Ruby on my Ubuntu machine, and did this:

我刚刚在我的Ubuntu机器上安装了Ruby,并做到了:

$ ruby --version
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]

$ ruby
p File.basename("foo")
p File.exist("foo")
"foo"
-:2:in `<main>': undefined method `exist' for File:Class (NoMethodError)

The way I read the File documentation page, shouldn't this work?

我读取文件文档页面的方式,不应该这样吗?

1 个解决方案

#1


8  

You forgot the question mark (?) at the end:

你忘了最后的问号(?)

File.exist? 'foo'
File.exists? 'foo'

In general, methods which answer questions will always end with a question mark.

一般来说,回答问题的方法总是以问号结尾。

In this case, the method is asking File the does 'foo' exist? question. The class will return the answer.

在这种情况下,方法是问文件是否存在foo ?的问题。课程将返回答案。

#1


8  

You forgot the question mark (?) at the end:

你忘了最后的问号(?)

File.exist? 'foo'
File.exists? 'foo'

In general, methods which answer questions will always end with a question mark.

一般来说,回答问题的方法总是以问号结尾。

In this case, the method is asking File the does 'foo' exist? question. The class will return the answer.

在这种情况下,方法是问文件是否存在foo ?的问题。课程将返回答案。