我已经定义了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):"`
A puts titel给出了正确的输出。
也许我错过了什么?
答案 0 :(得分:0)
这是因为模块@todo
中未定义/可用实例变量Checker
。
您可以使用以下方式验证:
module Checker
def self.check_dir
puts @todo
end
end
@todo = "hello"
Checker::check_dir
#
# => nil
Refer to this SO有关模块内如何提供实例变量的详细信息。
答案 1 :(得分:0)
我发现的另一件事是mkdir创建#{publican_doc_dir} / articles /#{title}?/#{title}。所以文章/ test01?/ test01而不是articles / test01。奇怪的是......