繁体   English   中英

红宝石:无法使用FileUtil创建目录

[英]ruby: Can't create directory with FileUtil

我已经定义了一个module

require 'dir'
require 'fileutils'
module Checker

  # Checks if the targetdirectory are present. If not, it creates one
  def self.check_dir
    # Checking if dir exists
    if Dir.exist?("#{@todo}")
      puts 'Found directory. Im using it.'
    else
      puts 'No directory found. Im creating it.'
      # Creates the new directory
      FileUtils.mkdir_p("#{@todo}")
        if Dir.exist?("#{@todo}")
          raise('Cant create directory')
        else
          puts 'Created new directory...'
        end
    end
  end
end

我在这里使用它:

puts "Erstellt Verzeichnis #{articles_dir}/#{titel}"
@todo = "#{articles_dir}/#{titel}"
Checker.check_dir

puts 'Wechsle in article Verzeichnis'
FileUtils.cd("#{articles_dir}/#{titel}") do

[...stuff...]

end

但是运行代码后,我得到:

Erstellt Verzeichnis
/home/sascha/Dokumente/Textdokumente/publican-dokumentation2/articles/TestEIns
/home/sascha/ownCloud/RubymineProjects/PublicanCreators/lib No
directory found. Im creating it.
/usr/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': No such file or
directory @ dir_s_mkdir -  (Errno::ENOENT)    from
/usr/lib/ruby/2.2.0/fileutils.rb:252:in `fu_mkdir'    from
/usr/lib/ruby/2.2.0/fileutils.rb:226:in `block (2 levels) in mkdir_p'
  from /usr/lib/ruby/2.2.0/fileutils.rb:224:in `reverse_each'     from
/usr/lib/ruby/2.2.0/fileutils.rb:224:in `block in mkdir_p'    from
/usr/lib/ruby/2.2.0/fileutils.rb:210:in `each'    from
/usr/lib/ruby/2.2.0/fileutils.rb:210:in `mkdir_p'     from
/home/sascha/RubymineProjects/PublicanCreators/lib/PublicanCreators/checker.rb:14:in
`check_dir'   from
/home/sascha/RubymineProjects/PublicanCreators/lib/PublicanCreators.rb:60:in
`<top (required)>'    from -e:1:in `load'     from -e:1:in `<main>' Process
finished with exit code 1

作为变量,我正在使用:

home = Dir.home
publican_doc_dir = "#{home}/Dokumente/Textdokumente/publican-dokumentation2"
articles_dir = "#{publican_doc_dir}/articles"
titel = `yad --entry --button="Bestätigen" --title="Neuen Artikel erstellen" --text="Gebe ein Titel ein (Mit Unterstrichen statt Leerzeichen und ohne Umlaute):"`

推杆毛刺为我提供了正确的输出。

也许我错过了什么?

这是因为实例变量@todo在模块Checker未定义/不可用。

您可以使用以下方法对此进行验证:

module Checker
 def self.check_dir
  puts @todo
 end
end

@todo = "hello"
Checker::check_dir
#
# => nil

有关在模块内部如何使用实例变量的详细信息, 请参考此SO

我发现的另一件事是mkdir创建了#{publican_doc_dir} / articles /#{title}?/#{title}。 因此,articles / test01?/ test01代替articles / test01。 奇怪的事情...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM