草庐IT

curr_file

全部标签

ruby - Ruby 中的 File.open、open 和 IO.foreach 有什么区别?

以下所有API都做同样的事情:打开一个文件并为每一行调用一个block。我们应该优先使用一个而不是另一个吗?File.open("file").each_line{|line|putsline}open("file").each_line{|line|putsline}IO.foreach("file"){|line|putsline} 最佳答案 这3个选择之间存在重要差异。File.open("file").each_line{|行|放置行File.open打开一个本地文件并返回一个文件对象文件保持打开状态,直到您对其调用IO#c

ruby-on-rails - 在 RubyMine 中运行规范会导致 "cannot load such file -- teamcity/spec/runner/formatter/teamcity/formatter (LoadError)"

操作系统:ArchLinux,Rails版本:4,RubyMine:6.3当我从Tools-RunRakeTask-spec运行规范时,我总是会收到此错误:/home/chylli/.rvm/gems/ruby-2.1.2@rails4/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1024:in`require':cannotloadsuchfile--teamcity/spec/runner/formatter/teamcity/formatter(LoadError)但可以运行“调试规范:模型”。我尝试了什么:我在运行

ruby - `if __FILE__ == $0` 在 Ruby 中是什么意思

if__FILE__==$0$:.unshiftFile.join(File.dirname(__FILE__),'..')我在Ruby中找到这段代码,什么意思? 最佳答案 #Onlyrunthefollowingcodewhenthisfileisthemainfilebeingrun#insteadofhavingbeenrequiredorloadedbyanotherfileif__FILE__==$0#Findtheparentdirectoryofthisfileandaddittothefront#ofthelisto

ruby-on-rails - 为什么我在安装 PaperClip 时得到一个 "undefined method for ` has_attached_file`?

我刚刚安装了Paperclip插件,但收到以下错误消息,但我不确定原因:NoMethodError(undefinedmethod`has_attached_file'for#):/Users/bgadoci/.gem/ruby/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in`method_missing'app/models/post.rb:2app/controllers/posts_controller.rb:50:in`show'它引用了will_paginategem。据我所知,我的PostsC

ruby-on-rails - 使用 send_file 从 Amazon S3 下载文件?

我的应用程序中有一个下载链接,用户应该可以从该链接下载存储在s3上的文件。这些文件将可通过类似以下形式的url公开访问https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png下载链接在我的Controller中点击了一个Action:classAttachmentsController但是当我尝试下载文件时出现以下错误:ActionController::MissingFileinAttachmentsController#showCannotreadfilehttps://s3.amazonaws.com/:bucket_na

ruby - `File` 对象的访问模式之间的差异(即 w+、r+)

在Ruby中使用文件时,r+和w+模式有什么区别?a+模式怎么样? 最佳答案 参见http://www.tutorialspoint.com/ruby/ruby_input_output.htm引用:rRead-onlymode.Thefilepointerisplacedatthebeginningofthefile.Thisisthedefaultmode.r+Read-writemode.Thefilepointerwillbeatthebeginningofthefile.wWrite-onlymode.Overwrites

ruby - 为什么 __FILE__ 是大写而 __dir__ 是小写?

在Ruby2.0.0-p0中,引入了__dir__变量,以便于访问当前正在执行的文件的目录。为什么__dir__是小写,而__FILE__是大写? 最佳答案 我认为这是因为__FILE__是一个解析时间常量,而__dir__是一个函数并返回File.dirname(File.realpath(__FILE__))有关详细信息,请参阅Thisdiscussion 关于ruby-为什么__FILE__是大写而__dir__是小写?,我们在StackOverflow上找到一个类似的问题:

ruby-on-rails - "bin/rails: No such file or directory"w/Heroku 上的 Ruby 2 和 Rails 4

同时遵循MichaelHartl的Rails4Beta版本RubyonRailsTutorial,我的应用程序无法在Heroku上启动,但可以在本地使用bundleexecrailsserver正常运行。检查herokulogs-t显示以下错误:$heroku[web.1]:Statechangedfromcrashedtostarting$heroku[web.1]:Startingprocesswithcommand`bin/railsserver-p33847-e$RAILS_ENV`$app[web.1]:bash:bin/rails:Nosuchfileordirectory

ruby-on-rails - '需要': cannot load such file -- 'nokogiri\nokogiri' (LoadError) when running `rails server`

我正在使用DevKit在Windows8.1上运行全新安装的Ruby2.2.1。安装后我运行:geminstallrailsrailsnewtestappcdtestapprailsserver保留其他所有默认值。进程在最后一行失败,我没有运行服务器,而是收到错误消息in'require':cannotloadsuchfile--'nokogiri\nokogiri'(LoadError)每次都会发生这种情况,我环顾四周并尝试了我发现的所有方法来修复它,但到目前为止没有任何效果。这里的问题是什么?如何让一个简单的测试Rails应用程序正常工作? 最佳答案

ruby-on-rails - Ruby on Rails 中的 send_data 和 send_file 有什么区别?

哪个最适合流式传输和文件下载?请举例说明。 最佳答案 send_data(_data_,options={})send_file(_path_,options={})此处的主要区别在于您使用send_data传递数据(二进制代码或其他)或使用send_file传递文件路径。因此您可以生成一些数据并将其作为内嵌文本或附件发送,而无需通过send_data在您的服务器上生成文件。或者您可以使用send_file发送准备好的文件data="HelloWorld!"send_data(data,:filename=>"my_file.txt