[英]Ruby undefined method `write' for IO:Class (NoMethodError)
when i run my ruby file 当我运行我的红宝石文件时
ruby test.rb
which has one line: 其中一行:
IO.write("testfile.txt","123")
i get 我得到
test.rb:1:in `<main>': undefined method `write' for IO:Class (NoMethodError)
Well, what kind of answer do you expect? 那么,您希望得到什么样的答案? IO
does not have any class method called write
. IO
没有任何称为write
类方法。 At most it has binwrite
and an instance method #write
. 它最多具有binwrite
和实例方法#write
。
So either you use binwrite
(http://rubydoc.info/stdlib/core/1.9.3/IO.binwrite) or you use the File
class and go the full way of 因此,您可以使用binwrite
(http://rubydoc.info/stdlib/core/1.9.3/IO.binwrite)或使用File
类并完全使用
File.open("testfile.txt", "w") { |f| f << "123" }
Edit: Apparently there is an IO.write
method beginning with Ruby 1.9.3. 编辑:显然有一个从Ruby 1.9.3开始的IO.write
方法。 There is, however, no such method in any earlier versions of 1.9 or 1.8. 但是,在任何较早的1.9或1.8版本中都没有这种方法。
There are a couple of issues, 有几个问题,
If you want to write to a file you should use the File class 如果要写入文件,则应使用File类
File.open("testfile.txt", "w") do |file| File.open(“ testfile.txt”,“ w”)做| file | file.write("123") end file.write(“ 123”)结束
I think you're probably just getting started with Ruby so it may be a good idea to read up a book on ruby which will show some of these basics. 我认为您可能刚开始使用Ruby,因此最好阅读一本有关红宝石的书籍,其中会介绍其中的一些基础知识。 I have used "The Ruby Programming Language" by David Flanagan and Matz, but quite a few people have used what is called the Pickaxe book or "Programming Ruby" by Dave Thomas, Chad Fowler and Andy Hunt. 我使用过David Flanagan和Matz的“ Ruby编程语言”,但是很多人使用了Dave Thomas,Chad Fowler和Andy Hunt所著的Pickaxe书或“ Programming Ruby”。
IO doesn't have a 'built in' write method. IO没有“内置”写入方法。 File.write should do this job File.write应该做这个工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.